summaryrefslogtreecommitdiffstats
path: root/src/dmilog.c
blob: acc465f8bd01291c7fbac6ea0c2c23bd22cbb3a7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
 *   2009 (C) David Sommerseth <davids@redhat.com>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
 *
 *   For the avoidance of doubt the "preferred form" of this code is one which
 *   is in an open unpatent encumbered format. Where cryptographic key signing
 *   forms part of the process of creating an executable the information
 *   including keys needed to generate an equivalently functional executable
 *   are deemed to be part of the source code.
 */

/**
 *  @file dmilog.h
 *  @brief A simple log module
 *  @author David Sommerseth <davids@redhat.com>
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>

#include "dmilog.h"

/**
 * Allocates memory for a new Log_t record
 *
 * @return Returns a pointer to a new Log_t record, otherwise NULL on error
 */
Log_t * log_init()
{
	Log_t *ret = NULL;

	ret = (Log_t *) calloc(1, sizeof(Log_t)+2);
	if( !ret ) {
		fprintf(stderr, "** ERROR **  Could not allocate memory for log data\n");
	}
	ret->level = -1; // Initialised - chain header pointer always have -1.
	return ret;
}




/**
 * Registers a new log entry
 *
 * @param logp   Pointer to an allocated Log_t record.  New records will be appended to the end
 * @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
 */
int log_append(Log_t *logp, 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);
		}
	}

        va_start(ap, fmt);
	if( !ptr || !ptr->next || !ptr->next->message ) {
		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;
}


/**
 * Retrieve all log entries in the Log_t record chain with the corresponding log level.
 * One string will be returned, with all log entries separated with newline.
 *
 * @param logp  Pointer to Log_t record chain with log data
 * @param level Log entries to retrieve
 *
 * @return Returns a pointer to a buffer with all log lines.  This must be freed after usage.
 */
char * log_retrieve(Log_t *logp, int level)
{
	char *ret = NULL;
	size_t len = 0;
	Log_t *ptr = NULL;

	if( !logp ) {
		return NULL;
	}

	for( ptr = logp; ptr != NULL; ptr = ptr->next ) {
		if( ptr && ptr->level == level ) {
			if( ret ) {
				ret = realloc(ret, strlen(ptr->message)+len+3);
			} else {
				ret = calloc(1, strlen(ptr->message)+2);
			}

			if( !ret ) {
				fprintf(stderr,
					"** ERROR ** Could not allocate log retrieval memory buffer\n");
				return NULL;
			}
			strcat(ret, ptr->message);
			strcat(ret, "\n");
			ptr->read++;
			len = strlen(ret);
		}
	}
	return ret;
}


/**
 * Remove only log records of a particular log level from the log chain.  Only
 * records that have been read (by using log_retrieve()) will be removed unless
 * the unread argument == 1.
 *
 * @param logp   Pointer to log chain to work on
 * @param level  Log level to remove
 * @param unread Set to 1 to also clear unread log entriesz
 *
 * @return Returns number of removed elements.
 */
size_t log_clear_partial(Log_t *logp, int level, int unread)
{
	Log_t *ptr = NULL, *prev = NULL;
	size_t elmnt = 0;

	if( !logp ) {
		return 0;
	}

	prev = logp;
	for( ptr = logp->next; ptr != NULL; ptr = ptr->next ) {
		if( !ptr ) {
			break;
		}

		// Only remove log entries which is of the expected log level
		// and that have been read.
		if( (ptr->level == level) && ((unread == 1) || (ptr->read > 0)) ) {
			prev->next = ptr->next;
			if( ptr->message ) {
				free(ptr->message);
				ptr->message = NULL;
			}
			free(ptr);
			ptr = prev;
			elmnt++;
		}
		prev = ptr;
	}

	return elmnt;
}


/**
 * Free all memory used by a Log_t pointer chain.
 *
 * @param logp Pointer to log entries to free up.
 */
void log_close(Log_t *logp)
{
	Log_t *ptr = NULL, *next = NULL;

	ptr = logp;
	while( ptr ) {
		next = ptr->next;
		ptr->next = NULL;
		if( ptr->message ) {
			free(ptr->message);
			ptr->message = NULL;
		}
		free(ptr);
		ptr = next;
	}
}