forked from dunst-project/dunst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.h
78 lines (67 loc) · 2.19 KB
/
log.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
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
/* copyright 2013 Sascha Kruse and contributors (see LICENSE for licensing information) */
#include <errno.h>
#include <glib.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef DUNST_LOG_H
#define DUNST_LOG_H
/**
* Prefix message with "[<source path>:<function name>:<line number>] "
*
* @param format is either a format string like the first argument
* of printf() or a string literal.
* @... are the arguments to above format string.
*
* This requires -Wno-gnu-zero-variadic-macro-arguments with clang
* because of token pasting ',' and %__VA_ARGS__ being a GNU extension.
* However, the result is the same with both gcc and clang and since we are
* compiling with '-std=gnu99', this should be fine.
*/
#if __GNUC__ >= 8 || __clang_major__ >= 6
#define MSG(format, ...) "[%16s:%04d] " format, __func__, __LINE__, ## __VA_ARGS__
#endif
#ifdef MSG
// These should benefit from more context
#define LOG_E(...) g_error(MSG(__VA_ARGS__))
#define LOG_C(...) g_critical(MSG(__VA_ARGS__))
#define LOG_D(...) g_debug(MSG(__VA_ARGS__))
#else
#define LOG_E g_error
#define LOG_C g_critical
#define LOG_D g_debug
#endif
#define LOG_W g_warning
#define LOG_M g_message
#define LOG_I g_info
#define DIE(...) do { LOG_C(__VA_ARGS__); exit(EXIT_FAILURE); } while (0)
// unified fopen() result messages
#define MSG_FOPEN_SUCCESS(path, fp) "'%s' open, fd: '%d'", path, fileno(fp)
#define MSG_FOPEN_FAILURE(path) "Cannot open '%s': '%s'", path, strerror(errno)
/**
* Set the current loglevel to `level`
*
* @param level The desired log level
*
* If `level` is `NULL`, nothing will be done.
* If `level` is an invalid value, nothing will be done.
*/
void log_set_level(GLogLevelFlags level);
/**
* Set the current loglevel to `level`
*
* @param level The desired log level as a string
*
* If `level` is `NULL`, nothing will be done.
* If `level` is an invalid value, nothing will be done.
*/
void log_set_level_from_string(const char* level);
/**
* Initialise log handling. Can be called any time.
*
* @param testing If we're in testing mode and should
* suppress all output
*/
void dunst_log_init(bool testing);
#endif
/* vim: set ft=c tabstop=8 shiftwidth=8 expandtab textwidth=0: */