I am looking for a productive and simple logging library for C, which can output the log to a file. Displaying messages in the log I want to make like this:
date-time tag message
It would be nice to control the level of detail of messages and control the size of the file.
I found two projects that are suitable for me. It log4c and nglogc.
log4c seemed too big. nglogc quite fit, but also has a redundant functional. maybe you tell me more variants?
A logging library (or logging framework) is code that you embed into your application to create and manage log events. Logging libraries provide APIs for creating, structuring, formatting, and transmitting log events in a consistent way. Like agents, they're used to send events from your application to a destination.
Logger is used for creating customized error log files or an error can be registered as a log entry in the Windows Event Log on the administrator's machine. This article is demonstrating how to create a text based error log file along with error messages with your own format, using a C# class.
You can use this
File logger.h
#ifndef LOGGER_H
#define LOGGER_H
void logger(const char* tag, const char* message);
#endif /* LOG_H */
File logger.c
#include "logger.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void logger(const char* tag, const char* message) {
time_t now;
time(&now);
printf("%s [%s]: %s\n", ctime(&now), tag, message);
}
It's probably not perfect, but it does satisfy the needs as you have presented them.
I suggest the log library which is written by myself --- zlog!
The way to fit your need in zlog is:
$ vi /etc/zlog.conf
[formats]
simple = "%D %c %m%n"
# don't know what the tag mean in your question, so put category of zlog instead
# log level is also available here, add %V means level
[rules]
my_cat.* "xxx.log"; simple
$ vi hello.c
#include <stdio.h>
#include "zlog.h"
int main(int argc, char** argv)
{
int rc;
zlog_category_t *c;
rc = dzlog_init("/etc/zlog.conf", "my_cat");
if (rc) {
printf("init failed\n");
return -1;
}
zlog_info(c, "hello, zlog");
zlog_fini();
return 0;
}
It will generate xxx.log in current directory as
2012-09-30 07:22:50 my_cat hello, zlog
Links:
Download: https://github.com/HardySimpson/zlog/archive/latest-stable.tar.gz
UsersGuide: http://hardysimpson.github.com/zlog/UsersGuide-EN.html
Hompage: http://hardysimpson.github.com/zlog/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With