File: log.c

package info (click to toggle)
djmount 0.71-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,148 kB
  • sloc: ansic: 37,570; sh: 9,371; xml: 637; makefile: 292; perl: 29
file content (229 lines) | stat: -rw-r--r-- 5,590 bytes parent folder | download | duplicates (4)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* $Id: log.c 157 2006-02-09 21:33:30Z r3mi $
 *
 * Log facilities.
 * This file is part of djmount.
 *
 * (C) Copyright 2005 Rmi Turboult <r3mi@users.sourceforge.net>
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */


#include "log.h"
#include <stdarg.h>
#include <stdio.h>
#include <upnp/ithread.h>
#include <stdbool.h>


/*
 * Function pointer to use for displaying formatted
 * strings. Set on Initialization of device. 
 */
static Log_PrintFunction gPrintFun = NULL;


/*
 * Mutex to control displaying of events
 */
static bool g_initialized = false;
static ithread_mutex_t g_log_mutex;


/*
 * Current log level
 */
static Log_Level g_max_level = LOG_INFO;


/*
 * Authorize colorizing log output
 */
static bool g_colorize = false;

// ANSI Color codes
#define VT(CODES)		"\033[" CODES "m"
#define VT_NORMAL 		VT("0")
#define VT_DIM			VT("2")
#define VT_RED			VT("31")
#define VT_RED_BRIGHT		VT("31;1")
#define VT_GREEN 		VT("32")
#define VT_YELLOW_BRIGHT	VT("33;1")
#define VT_BLUE 		VT("34")
#define VT_MAGENTA_BRIGHT	VT("35;1")

static const char* const COLORS[] = {
	[LOG_ERROR]   = VT_RED_BRIGHT,
	[LOG_WARNING] = VT_MAGENTA_BRIGHT,
	[LOG_INFO]    = VT_BLUE,
	[LOG_DEBUG]   = VT_DIM,
};
static int const NB_COLORS = sizeof(COLORS)/sizeof(COLORS[0]);

static const char* const COLOR_UNKNOWN_LEVEL = VT_RED_BRIGHT;



/*****************************************************************************
 * Log_Initialize
 *****************************************************************************/

int
Log_Initialize (Log_PrintFunction print_function )
{
	if (! g_initialized) {
		ithread_mutexattr_t attr;
		
		ithread_mutexattr_init (&attr);
		ithread_mutexattr_setkind_np (&attr,
					      ITHREAD_MUTEX_RECURSIVE_NP);
		ithread_mutex_init (&g_log_mutex, &attr);
		ithread_mutexattr_destroy (&attr);
		g_initialized = true;
	}
	gPrintFun = print_function;
	return 0;
}


/*****************************************************************************
 * Log_Finish
 *****************************************************************************/
int
Log_Finish ()
{
	gPrintFun = NULL;
	if (g_initialized) {
		g_initialized = false;
		ithread_mutex_destroy (&g_log_mutex);
	}
	return 0;
}


/*****************************************************************************
 * Log_IsActivated
 *****************************************************************************/
bool
Log_IsActivated (Log_Level level) 
{
	return ( g_initialized && gPrintFun && level <= g_max_level );
}


/*****************************************************************************
 * Log_Print 
 *****************************************************************************/
int
Log_Print (Log_Level level, const char* msg)
{
	if (Log_IsActivated (level) && msg) { 
		ithread_mutex_lock (&g_log_mutex);
		gPrintFun (level, msg);
		ithread_mutex_unlock (&g_log_mutex);
	}
	return 0;
}


/*****************************************************************************
 * Log_Printf
 *****************************************************************************/
int
Log_Printf (Log_Level level, const char* fmt, ... )
{
	if (Log_IsActivated (level) && fmt) { 
		va_list ap;
		char buf[4096] = "";
		
		va_start (ap, fmt);
		int rc = vsnprintf (buf, sizeof(buf), fmt, ap);
		va_end (ap);
		
		if (rc >= 0) {
			ithread_mutex_lock (&g_log_mutex);
			gPrintFun (level, buf);
			ithread_mutex_unlock (&g_log_mutex);
		}
		return rc;
	}
	return -1;
}



/*****************************************************************************
 * Log_SetMaxLevel
 *****************************************************************************/
void 
Log_SetMaxLevel (Log_Level max_level)
{
	g_max_level = max_level;
}


/*****************************************************************************
 * Log_Lock / Log_Unlock
 *****************************************************************************/

int
Log_Lock()
{
	return ithread_mutex_lock (&g_log_mutex);
}

int
Log_Unlock()
{
	return ithread_mutex_unlock (&g_log_mutex);
}


/*****************************************************************************
 * Log_Colorize / Log_BeginColor / Log_EndColor
 *****************************************************************************/
void
Log_Colorize (bool colorize)
{
	g_colorize = colorize;
}

void 
Log_BeginColor (Log_Level level, FILE* stream)
{
	if (Log_IsActivated (level) && stream) { 
		// colorize ?
		const bool colorize = g_colorize && isatty (fileno (stream)); 
		if (colorize) {
			if (level >= 0 && level <= NB_COLORS && COLORS[level])
				fputs (COLORS[level], stream);
			else
				fputs (COLOR_UNKNOWN_LEVEL, stream);
		}
	}
}

void 
Log_EndColor (Log_Level level, FILE* stream)
{
	if (Log_IsActivated (level) && stream) { 
		const bool colorize = g_colorize && isatty (fileno (stream)); 
		if (colorize) {
			fputs (VT_NORMAL, stream);
		}
	}
}