Search Suggest

Logging

Log file là một file rất quan trong nên có cho mỗi chương trình để lưu lại tất cả thông tin về quá trình chạy của chương trình, từ đó có thể phân tích kiểm tra thông tin history, tìm kiếm lỗi nếu xảy ra, ...

1. syslog
 #include <syslog.h>  
void syslog(int priority, const char *message, arguments...);

syslog gửi log mesage đến file log hệ thống /var/log/messages và /var/log/debug bạn có thể dùng lệnh cat để xem log msg.

+ priority
Bitwire OR của level và facility value.

level:

Priority Level Description
LOG_EMERG An emergency situation
LOG_ALERT High-priority problem, such as database corruption
LOG_CRIT Critical error, such as hardware failure
LOG_ERR Errors
LOG_WARNING Warning
LOG_NOTICE Special conditions requiring attention
LOG_INFO Informational messages
LOG_DEBUG Debug messages

facility value:

Facility values Description
LOG_USER the message has come from a user application
LOG_LOCAL0 which can be assignedmeanings by the local administrator
... ...
LOG_LOCAL7

Tùy thuộc vào priority mà syslog sẽ có cách xử lý khác nhau đối với message, ví dụ với LOG_EMERG thì msg được gửi đến tất cả các user, LOG_ALERT thì msg được gửi mail đến administrator, LOG_DEBUG thì msg được bỏ qua, còn các trường hợp khác sẽ ghi vaog log file.

+ message
Message được syslog gửi đi.
+ arguments
Các đối số khác.
+ return

Ex:
syslog.c
   
#include <syslog.h>
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *f;
f = fopen("not_here","r");
if(!f)
syslog(LOG_ERR|LOG_USER,"oops hi hi ha ha - %m\n");
exit(0);
}


Compile & Execute (root user):
 $ gcc syslog.c  
$ su
# ./a.out
# cat /var/log/messages
Aug 2 11:24:00 localhost a.out: oops - No such file or directory

2. openlog
 #include <syslog.h>  
void closelog(void);
void openlog(const char *ident, int logopt, int facility);
int setlogmask(int maskpri);

syslog không cho biết thông tin về user nào, chương trình nào tạo ra log msg, mà chỉ có một khuôn dạng log chung, vì thế trong nhiều trường hợp sẽ khó khăn để fixbug khi chương trình có lỗi. Chính vì thế cần có thêm các hàm để thay đổi hành vi của logger.

openlog mở logger ra để chỉnh sửa:
+ ident
Chuỗi string tùy ý bạn thêm vào dòng log msg.
+ logopt
log option của openlog:
 /*  
* Option flags for openlog.
*
* LOG_ODELAY no longer does anything.
* LOG_NDELAY is the inverse of what it used to be.
*/
#define LOG_PID 0x01 /* log the pid with each message */
#define LOG_CONS 0x02 /* log on the console if errors in sending */
#define LOG_ODELAY 0x04 /* delay open until first syslog() (default) */
#define LOG_NDELAY 0x08 /* don't delay open */
#define LOG_NOWAIT 0x10 /* don't wait for console forks: DEPRECATED */
#define LOG_PERROR 0x20 /* log to stderr as well */

+ facility
Tương tự như trên syslog.

setlogmask điều khiển priority level của syslog:
+ maskpri
Set môt priority với LOG_MASK(priority) hoặc một dải priority với LOG_UPTO(priority)

Ex:
logmask.c
 #include <syslog.h>  
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

int main()
{
int logmask;
openlog("logmask", LOG_PID|LOG_CONS, LOG_USER);

/*
* ghi ra /var/log/messages
*
*/
syslog(LOG_INFO,"informative message, pid = %d", getpid());

/*
* ghi ra /var/log/debug
*
*/
syslog(LOG_DEBUG,"debug message, should appear");

/*
* Bỏ qua tất cả các msg có priority < LOG_NOTICE
*
*/
logmask = setlogmask(LOG_UPTO(LOG_NOTICE));
syslog(LOG_DEBUG,"debug message, should not appear");

exit(0);
}


Compile & Execute:
 $ gcc logmask.c   
$ su
# ./a.out
# cat /var/log/messages
Aug 2 22:14:29 localhost logmask[4676]: informative message, pid = 4676




Đăng nhận xét