File: my_log.c

package info (click to toggle)
fprobe 1.1-7.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch, wheezy
  • size: 728 kB
  • ctags: 444
  • sloc: sh: 3,164; ansic: 2,177; makefile: 16
file content (57 lines) | stat: -rw-r--r-- 1,305 bytes parent folder | download | duplicates (11)
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
/*
	Copyright (C) Slava Astashonok <sla@0n.ru>

	This program is free software; you can redistribute it and/or
	modify it under the terms of the GNU General Public License.

	$Id: my_log.c,v 1.3.2.2 2004/02/02 08:06:24 sla Exp $
*/

#include <common.h>

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

#include <my_log.h>

static char *my_log_indent;
static unsigned my_log_min_level;
static unsigned my_log_flags;
static char *my_log_names[] = {
	"EMERG", "ALERT", "CRIT", "ERR", "WARNING", "NOTICE", "INFO", "DEBUG"
};

void my_log_open(char *indent, unsigned min_level, unsigned flags)
{
	my_log_indent = indent;
	my_log_min_level = min_level;
	my_log_flags = flags;
	//openlog(0, LOG_PID, MY_LOG_SYSLOG_FACILITY);
	openlog(my_log_indent, 0, MY_LOG_SYSLOG_FACILITY);
}

void my_log_close(void)
{
	closelog();
}

void my_log(unsigned level, const char *format, ...)
{
	va_list args;
	char msg[256];
	char msg_prefix[64];

	if (level <= my_log_min_level) {
		va_start(args, format);
		vsnprintf(msg, sizeof(msg), format, args);
		snprintf(msg_prefix, sizeof(msg_prefix), "[%s]: ", my_log_names[level]);

		if (my_log_flags & MY_LOG_SYSLOG)
			syslog(level, "%s%s", msg_prefix, msg);

		if (my_log_flags & MY_LOG_STDOUT)
			fprintf(stdout, "%s%s\n", msg_prefix, msg);
	}
}