Solaris下利用/proc文件系统特性简单构建log系统
1
、
syslogd
Unix下提供了syslogd的守护进程,并提供了一些接口函数,供用户进程使用,将系统日志和用户空间log写入/etc/syslog.conf所限定的文件中,一般Solaris下为/usr/adm/messages,Linux下为/var/log/messages。
其中控制系统log
接口组函数(位于/usr/include/syslog.h)为:
void openlog(const char *ident, int logopt, int facility);
说明:
改变日志信息的表示方法,并打开一个文件描述符。
参数:
ident – 指定字符串,该字符串会加在日志信息的前面,我们可以通过它来指明是哪个进程创建了这个log信息。
logopt – 对后续syslog调用行为进行设置,它是0个或几个位值的组合。
・ LOG_PID在日志信息中包含进程标识符,这是系统分配给每个进程的一个唯一值
・ LOG_CONS
如果信息不能被记录到日志文件中,就把它们发送到控制台
・ LOG_ODELAY
在第一次调用syslog时才打开日志功能
・ LOG_NDELAY
立即打开日志功能,而不是等到第一次记录日志时
facility – 指定什么样的程序或什么样级别的程序在记录log信息。
・ LOG_AUTHPRIV : 用于安全/授权讯息
・ LOG_CRON : 用于守护进程crond及at调度作用命令
・ LOG_DAEMON : 用于其他系统守护进程
・ LOG_KERN : 内核日志信息
・ LOG_LOCAL0~LOG_LOCAL7 : 保留
・ LOG_SYSLOG : syslogd内部所产生的日志信息
・ LOG_USER(default) : 一般使用者等级的日志信息
・ LOG_MAIL : 用于mail日志信息
void syslog(int priority, const char *message, .../* arguments */);
说明:
向syslogd进程发送日志信息,能直接调用而不经过openlog函数,本身负责日志文件描述符的开关工作。
参数:
priority - 该参数是一个严重级别与一个设施值的按位或。严重级别控制日志信息的处理,设施值记录日志信息的来源。
按照严重级别分类:
・ LOG_EMERG
紧急情况
・ LOG_ALERT
高优先级故障,例如数据库崩溃
・ LOG_CRIT
严重错误,例如硬件故障
・ LOG_ERR
错误
・ LOG_WARNING
警告
・ LOG_NOTICE
需要注意的特殊情况
・ LOG_INFO
一般信息
・ LOG_DEBUG
调试信息
message – 要写入syslog的信息(支持变参数)。
void closelog(void);
说明:关闭有openlog打开的文件描述符。
int setlogmask(int maskpri);
说明:设置一个日志掩码,并控制日志信息的优先级。优先级未在日志掩码中置位的后续syslog调用都将被丢弃。
例如:关闭LOG_DEBUG消息而不用改变程序主体。我们可以用
LOG_MASK(priority)为日志信息创建一个掩码,它的作用是创建一个只包含一个优先级的掩码。我们还可以用
LOG_UPTO(priority)来创建一个由指定优先级之前的所有优先级(包括指定优先级)构成的掩码。
2
、
proc
文件系统
/proc文件系统是一种虚拟的文件系统,向用户提供了内核的实时镜像,可以通过一种新的方法进行用户空间和内核空间的通信。与普通文件不同的是,这些文件是动态创建的。
3、代码实例
write_log.c
(取得进程名,通过/proc文件系统,方便每个App都可以调用这个接口,而是简单的传入参数)
。
/**/
/*
* Note: write_log.c - Application level log function.
*/
#include < stdio.h >
#include < time.h >
#include < unistd.h >
#include < stdlib.h >
#include < string.h >
#include < sys / types.h >
#include < sys / procfs.h >
#include < sys / stat.h >
#include < fcntl.h >
#include < stdarg.h >
#include " write_log.h "
#define LOG_BUFSIZE 1024
#define BUFSIZE 128
int write_log( char * log_str, ...)
... {
FILE *fp;
size_t written_len;
char log_messages[LOG_BUFSIZE];
char v_buffer[LOG_BUFSIZE];
char ps_name[BUFSIZE];
char *time_str;
char *log_file = "./asylog";
memset(log_messages, 0, LOG_BUFSIZE);
memset(v_buffer, 0, LOG_BUFSIZE);
memset(ps_name, 0, BUFSIZE);
/**//* get local time */
time_t local_time;
local_time = time(NULL);
time_str = ctime(&local_time);
/**//* delete char ' ' */
char time_buffer[BUFSIZE];
memset(time_buffer, 0, BUFSIZE);
memcpy(time_buffer, time_str, strlen(time_str) - 1);
/**//* handle variable argument list */
va_list v_args;
va_start(v_args, log_str);
(void)vsprintf(v_buffer, log_str, v_args);
va_end(v_args);
if ((fp = fopen(log_file, "a+")) == NULL) ...{
exit(1);
}
/**//* get process exe name */
get_psinfo(ps_name);
(void)sprintf(log_messages, "At %s [ps: %s - pid: %d] written log: %s",
time_buffer, ps_name, getpid(), v_buffer);
written_len = strlen(log_messages);
fwrite(log_messages,written_len , 1, fp);
fclose(fp);
return 0;
}
int get_psinfo( char * ps_name)
... {
int fd;
char psinfo_name[BUFSIZE];
/**//* process ps<1> infomation file - /proc/<pid>/psinfo */
prpsinfo_t prpsinfo;
/**//* process status file - /proc/<pid>/status */
prstatus_t prstatus;
memset(psinfo_name, 0, BUFSIZE);
(void)sprintf(psinfo_name, "/proc/%d", getpid());
if ((fd = open(psinfo_name, O_RDONLY)) == -1) ...{
printf("open proc filesystem error... ");
exit(-1);
}
/**//* get process info from proc filesystem */
if (ioctl(fd, PIOCPSINFO, &prpsinfo) < 0) ...{
exit(-1);
}
/**//* get process status from proc filesystem */
if (ioctl(fd, PIOCSTATUS, &prstatus) < 0 ) ...{
exit(-1);
}
memcpy(ps_name, prpsinfo.pr_fname, sizeof(prpsinfo.pr_fname));
return 0;
}
* Note: write_log.c - Application level log function.
*/
#include < stdio.h >
#include < time.h >
#include < unistd.h >
#include < stdlib.h >
#include < string.h >
#include < sys / types.h >
#include < sys / procfs.h >
#include < sys / stat.h >
#include < fcntl.h >
#include < stdarg.h >
#include " write_log.h "
#define LOG_BUFSIZE 1024
#define BUFSIZE 128
int write_log( char * log_str, ...)
... {
FILE *fp;
size_t written_len;
char log_messages[LOG_BUFSIZE];
char v_buffer[LOG_BUFSIZE];
char ps_name[BUFSIZE];
char *time_str;
char *log_file = "./asylog";
memset(log_messages, 0, LOG_BUFSIZE);
memset(v_buffer, 0, LOG_BUFSIZE);
memset(ps_name, 0, BUFSIZE);
/**//* get local time */
time_t local_time;
local_time = time(NULL);
time_str = ctime(&local_time);
/**//* delete char ' ' */
char time_buffer[BUFSIZE];
memset(time_buffer, 0, BUFSIZE);
memcpy(time_buffer, time_str, strlen(time_str) - 1);
/**//* handle variable argument list */
va_list v_args;
va_start(v_args, log_str);
(void)vsprintf(v_buffer, log_str, v_args);
va_end(v_args);
if ((fp = fopen(log_file, "a+")) == NULL) ...{
exit(1);
}
/**//* get process exe name */
get_psinfo(ps_name);
(void)sprintf(log_messages, "At %s [ps: %s - pid: %d] written log: %s",
time_buffer, ps_name, getpid(), v_buffer);
written_len = strlen(log_messages);
fwrite(log_messages,written_len , 1, fp);
fclose(fp);
return 0;
}
int get_psinfo( char * ps_name)
... {
int fd;
char psinfo_name[BUFSIZE];
/**//* process ps<1> infomation file - /proc/<pid>/psinfo */
prpsinfo_t prpsinfo;
/**//* process status file - /proc/<pid>/status */
prstatus_t prstatus;
memset(psinfo_name, 0, BUFSIZE);
(void)sprintf(psinfo_name, "/proc/%d", getpid());
if ((fd = open(psinfo_name, O_RDONLY)) == -1) ...{
printf("open proc filesystem error... ");
exit(-1);
}
/**//* get process info from proc filesystem */
if (ioctl(fd, PIOCPSINFO, &prpsinfo) < 0) ...{
exit(-1);
}
/**//* get process status from proc filesystem */
if (ioctl(fd, PIOCSTATUS, &prstatus) < 0 ) ...{
exit(-1);
}
memcpy(ps_name, prpsinfo.pr_fname, sizeof(prpsinfo.pr_fname));
return 0;
}
write_log.h
main_log.c
/**/
/*
* Note: write_log.h
*/
#ifndef WRITE_LOG_H
#define WRITE_LOG_H
#ifdef __cplusplus
extern " C " ... {
#endif
int get_psinfo(char *ps_name);
int write_log(char *log_str, ...);
#ifdef __cplusplus
}
#endif
#endif
* Note: write_log.h
*/
#ifndef WRITE_LOG_H
#define WRITE_LOG_H
#ifdef __cplusplus
extern " C " ... {
#endif
int get_psinfo(char *ps_name);
int write_log(char *log_str, ...);
#ifdef __cplusplus
}
#endif
#endif
main_log.c
/**/
/*
* Note: main_log.c - write log main.
*/
#include < stdio.h >
#include " write_log.h "
int main( int argc, char * argv[])
... {
write_log("write log [%s] [%s]. ", "asdf", "1234");
printf("write log done... ");
return 0;
}
* Note: main_log.c - write log main.
*/
#include < stdio.h >
#include " write_log.h "
int main( int argc, char * argv[])
... {
write_log("write log [%s] [%s]. ", "asdf", "1234");
printf("write log done... ");
return 0;
}
还有很多缺陷,继续更新中。。。。