- add timestamping support via log_tsprintf()
This commit is contained in:
parent
58bd6213eb
commit
3ca12815f5
43
logfile.c
43
logfile.c
@ -98,21 +98,46 @@ log_reopen(log_t **log)
|
||||
void
|
||||
log_printf(const log_t *log, const char *format,...)
|
||||
{
|
||||
if (!log_isopen(log))
|
||||
return;
|
||||
|
||||
va_list args;
|
||||
char *message;
|
||||
char *newline = "\n";
|
||||
|
||||
/*
|
||||
* todo: add timestamp and \n example: "Oct 21 13:46:35 %s\n"
|
||||
*/
|
||||
va_start(args, format);
|
||||
vasprintf(&message, format, args);
|
||||
va_end(args);
|
||||
|
||||
if (log_isopen(log)) {
|
||||
write(log->fd, message, strnlen(message, MAX_MSG_SIZE));
|
||||
write(log->fd, newline, sizeof(*newline));
|
||||
}
|
||||
|
||||
va_start(args, format);
|
||||
vasprintf(&message, format, args);
|
||||
va_end(args);
|
||||
void
|
||||
log_tsprintf(const log_t *log, const char *format,...)
|
||||
{
|
||||
if (!log_isopen(log))
|
||||
return;
|
||||
|
||||
write(log->fd, message, strnlen(message, MAX_MSG_SIZE));
|
||||
}
|
||||
va_list args;
|
||||
char *message;
|
||||
char s_time[30];
|
||||
time_t now;
|
||||
struct tm *ltime;
|
||||
size_t tsize;
|
||||
char *newline = "\n";
|
||||
|
||||
va_start(args, format);
|
||||
vasprintf(&message, format, args);
|
||||
va_end(args);
|
||||
|
||||
now = time(NULL);
|
||||
ltime = localtime(&now);
|
||||
tsize = strftime(s_time, sizeof(s_time), "%Y-%m-%d %T %Z - ", ltime);
|
||||
|
||||
write(log->fd, s_time, tsize);
|
||||
write(log->fd, message, strnlen(message, MAX_MSG_SIZE));
|
||||
write(log->fd, newline, sizeof(*newline));
|
||||
}
|
||||
|
||||
bool
|
||||
|
@ -40,6 +40,7 @@
|
||||
#include <errno.h>
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
#define LOGPATH "/var/log"
|
||||
#define MAX_MSG_SIZE 65536
|
||||
@ -58,5 +59,6 @@ bool log_isopen(const log_t *log);
|
||||
bool log_verify(const log_t *log);
|
||||
void log_reopen(log_t **log);
|
||||
void log_printf(const log_t *log, const char *format,...);
|
||||
void log_tsprintf(const log_t *log, const char *format,...);
|
||||
|
||||
#endif /* _LOGFILE_H */
|
||||
|
@ -42,20 +42,22 @@ main(void)
|
||||
if ((lh = log_open("test.log", 0600)) == NULL) {
|
||||
err(EX_IOERR, "Cannot open log file");
|
||||
}
|
||||
|
||||
if (!log_verify(lh))
|
||||
err(errno, "Failed to verify integrity of log file");
|
||||
|
||||
log_printf(lh, "opened file handle: %d , inode: %llu\n", lh->fd, lh->ino);
|
||||
log_printf(lh, "opened file handle: %d , inode: %llu", lh->fd, lh->ino);
|
||||
printf("logfile: %s, handle: %d, inode: %llu, mode: %d\n", lh->path, lh->fd, lh->ino, lh->mode);
|
||||
|
||||
log_reopen(&lh);
|
||||
if (!log_verify(lh))
|
||||
err(errno, "Failed to verify integrity of reopened log file");
|
||||
|
||||
log_printf(lh, "reopened file handle: %d , inode: %llu\n", lh->fd, lh->ino);
|
||||
log_printf(lh, "reopened file handle: %d , inode: %llu", lh->fd, lh->ino);
|
||||
printf("logfile: %s, handle: %d, inode: %llu, mode: %d\n", lh->path, lh->fd, lh->ino, lh->mode);
|
||||
|
||||
for (int i = 1; i <= 4; i++)
|
||||
log_tsprintf(lh, "This is a time stamped message %d", i);
|
||||
|
||||
log_close(lh);
|
||||
|
||||
return 0;
|
||||
|
Loading…
x
Reference in New Issue
Block a user