summaryrefslogtreecommitdiffstats
path: root/src/dmilog.c
diff options
context:
space:
mode:
authorDavid Sommerseth <davids@redhat.com>2010-02-05 15:33:39 +0100
committerDavid Sommerseth <davids@redhat.com>2010-02-05 15:33:39 +0100
commit5ebf67efee2893c4d3dc60ec31a0efeb0894d245 (patch)
tree337c4ff23b1498096e64179dadddf8b39a81e340 /src/dmilog.c
parent24af40698f1486c899333ea1f129e87590cdee7e (diff)
downloadpython-dmidecode-5ebf67efee2893c4d3dc60ec31a0efeb0894d245.tar.gz
python-dmidecode-5ebf67efee2893c4d3dc60ec31a0efeb0894d245.tar.xz
python-dmidecode-5ebf67efee2893c4d3dc60ec31a0efeb0894d245.zip
Added logging flag to log_append() to change log behaviour
Valid flags are: * LOGFL_NORMAL Log all messages to the log context, and send log message to stderr on errors * LOGFL_NODUPS Log only unique messages. Duplicated messages will be removed * LOGFL_NOSTDERR Don't write to stderr, even if errors occur
Diffstat (limited to 'src/dmilog.c')
-rw-r--r--src/dmilog.c79
1 files changed, 42 insertions, 37 deletions
diff --git a/src/dmilog.c b/src/dmilog.c
index e9b89e8..f7ade5d 100644
--- a/src/dmilog.c
+++ b/src/dmilog.c
@@ -59,51 +59,56 @@ Log_t * log_init()
* Registers a new log entry
*
* @param logp Pointer to an allocated Log_t record. New records will be appended to the end
+ * @param flags Log flags, to specify logging behaviour
* @param level syslog log level values. LOG_ERR and LOG_WARNING are allowed
* @param fmt stdarg based string with the log contents
*
* @return Returns 1 on successful registration of log entry, otherwise -1 and error is printed to stderr
+ * unless LOGFL_NOSTDERR is set in flags.
*/
-int log_append(Log_t *logp, int level, const char *fmt, ...)
+int log_append(Log_t *logp, Log_f flags, int level, const char *fmt, ...)
{
- Log_t *ptr = NULL;
- va_list ap;
-
- // Go the end of the record chain
- ptr = logp;
- while( ptr && ptr->next ) {
- ptr = ptr->next;
- }
-
-
- if( ptr && ((level == LOG_ERR) || (level == LOG_WARNING)) ) {
- ptr->next = log_init();
- if( ptr->next ) {
- ptr->next->message = (char *) calloc(1, 4098);
- }
- }
+ Log_t *ptr = NULL;
+ va_list ap;
+ char logmsg[4098];
+ // Prepare log message
+ memset(&logmsg, 0, 4098);
va_start(ap, fmt);
- if( !ptr || !ptr->next || !ptr->next->message ) {
- if( logp ) {
- // Only print this if we logp is pointing somewhere.
- // If it is NULL, the caller did not establish a log
- // buffer on purpose (like dmidump.c) - thus this is
- // not an error with saving the log entry.
- fprintf(stderr, "** ERROR ** Failed to save log entry\n");
- }
- vfprintf(stderr, fmt, ap);
- fprintf(stderr, "\n");
- va_end(ap);
- return -1;
- }
-
- ptr->next->level = level;
- vsnprintf(ptr->next->message, 4096, fmt, ap);
- ptr->next->message = realloc(ptr->next->message, strlen(ptr->next->message)+2);
- va_end(ap);
-
- return 1;
+ vsnprintf(logmsg, 4096, fmt, ap);
+ va_end(ap);
+
+ // Go the end of the record chain
+ ptr = logp;
+ while( ptr && ptr->next ) {
+ // Ignore duplicated messages if LOGFL_NODUPS is set
+ if( (flags & LOGFL_NODUPS) && ptr->next && ptr->next->message
+ && (strcmp(ptr->next->message, logmsg) == 0) ) {
+ return 1;
+ }
+ ptr = ptr->next;
+ }
+
+ if( ptr && ((level == LOG_ERR) || (level == LOG_WARNING)) ) {
+ ptr->next = log_init();
+ if( ptr->next ) {
+ ptr->next->level = level;
+ ptr->next->message = strdup(logmsg);
+ return 1;
+ }
+ }
+
+ if( !(flags & LOGFL_NOSTDERR) ) {
+ if( logp ) {
+ // Only print this if we logp is pointing somewhere.
+ // If it is NULL, the caller did not establish a log
+ // buffer on purpose (like dmidump.c) - thus this is
+ // not an error with saving the log entry.
+ fprintf(stderr, "** ERROR ** Failed to save log entry\n");
+ }
+ fprintf(stderr, "%s\n", logmsg);
+ }
+ return -1;
}