-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogger.h
54 lines (46 loc) · 1.95 KB
/
logger.h
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
/*
* Copyright (c) 2016 Mark Heily <[email protected]>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#pragma once
extern "C" {
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <syslog.h>
}
/* Logging */
extern FILE *logfile;
#define _log_all_format "%14s:%-4d %-30s "
#define _log_all(level, format,...) do { \
if (logfile != NULL) { \
fprintf(logfile, "" _log_all_format "" format "\n", \
__FILE__, __LINE__, __PRETTY_FUNCTION__, ## __VA_ARGS__); \
} else { \
syslog(level, "" _log_all_format "" format "\n", \
__FILE__, __LINE__,__PRETTY_FUNCTION__, ## __VA_ARGS__); \
} \
} while (0)
#define log_error(format,...) _log_all(LOG_ERR, "**ERROR** " format, ## __VA_ARGS__)
#define log_warning(format,...) _log_all(LOG_WARNING, "WARNING: " format, ## __VA_ARGS__)
#define log_notice(format,...) _log_all(LOG_NOTICE, format, ## __VA_ARGS__)
#define log_info(format,...) _log_all(LOG_INFO, format, ## __VA_ARGS__)
#if !defined(NDEBUG)
#define log_debug(format,...) _log_all(LOG_DEBUG, format, ## __VA_ARGS__)
#else
#define log_debug(format,...) do { (void)format; } while (0)
#endif
#define log_errno(format,...) _log_all(LOG_ERR, format ": errno=%d (%s)", ## __VA_ARGS__, errno, strerror(errno))
void log_freopen(FILE *new_logfile);