- 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
|
void
|
||||||
log_printf(const log_t *log, const char *format,...)
|
log_printf(const log_t *log, const char *format,...)
|
||||||
{
|
{
|
||||||
|
if (!log_isopen(log))
|
||||||
|
return;
|
||||||
|
|
||||||
va_list args;
|
va_list args;
|
||||||
char *message;
|
char *message;
|
||||||
|
char *newline = "\n";
|
||||||
|
|
||||||
/*
|
va_start(args, format);
|
||||||
* todo: add timestamp and \n example: "Oct 21 13:46:35 %s\n"
|
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);
|
void
|
||||||
vasprintf(&message, format, args);
|
log_tsprintf(const log_t *log, const char *format,...)
|
||||||
va_end(args);
|
{
|
||||||
|
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
|
bool
|
||||||
|
@ -40,6 +40,7 @@
|
|||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <time.h>
|
||||||
|
|
||||||
#define LOGPATH "/var/log"
|
#define LOGPATH "/var/log"
|
||||||
#define MAX_MSG_SIZE 65536
|
#define MAX_MSG_SIZE 65536
|
||||||
@ -58,5 +59,6 @@ bool log_isopen(const log_t *log);
|
|||||||
bool log_verify(const log_t *log);
|
bool log_verify(const log_t *log);
|
||||||
void log_reopen(log_t **log);
|
void log_reopen(log_t **log);
|
||||||
void log_printf(const log_t *log, const char *format,...);
|
void log_printf(const log_t *log, const char *format,...);
|
||||||
|
void log_tsprintf(const log_t *log, const char *format,...);
|
||||||
|
|
||||||
#endif /* _LOGFILE_H */
|
#endif /* _LOGFILE_H */
|
||||||
|
@ -42,20 +42,22 @@ main(void)
|
|||||||
if ((lh = log_open("test.log", 0600)) == NULL) {
|
if ((lh = log_open("test.log", 0600)) == NULL) {
|
||||||
err(EX_IOERR, "Cannot open log file");
|
err(EX_IOERR, "Cannot open log file");
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!log_verify(lh))
|
if (!log_verify(lh))
|
||||||
err(errno, "Failed to verify integrity of log file");
|
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);
|
printf("logfile: %s, handle: %d, inode: %llu, mode: %d\n", lh->path, lh->fd, lh->ino, lh->mode);
|
||||||
|
|
||||||
log_reopen(&lh);
|
log_reopen(&lh);
|
||||||
if (!log_verify(lh))
|
if (!log_verify(lh))
|
||||||
err(errno, "Failed to verify integrity of reopened log file");
|
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);
|
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);
|
log_close(lh);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user