cfengine  3.15.4
About: CFEngine is a configuration management system for configuring and maintaining Unix-like computers (using an own high level policy language). Community version.
  Fossies Dox: cfengine-3.15.4.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

logging.h
Go to the documentation of this file.
1 /*
2  Copyright 2020 Northern.tech AS
3 
4  This file is part of CFEngine 3 - written and maintained by Northern.tech AS.
5 
6  This program is free software; you can redistribute it and/or modify it
7  under the terms of the GNU General Public License as published by the
8  Free Software Foundation; version 3.
9 
10  This program is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with this program; if not, write to the Free Software
17  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
18 
19  To the extent this program is licensed as part of the Enterprise
20  versions of CFEngine, the applicable Commercial Open Source License
21  (COSL) may apply to this file if you as a licensee so wish it. See
22  included file COSL.txt.
23 */
24 
25 
26 #ifndef CFENGINE_LOGGING_H
27 #define CFENGINE_LOGGING_H
28 
29 
30 #include <platform.h>
31 #include <compiler.h>
32 
33 
34 // Does not include timezone, since it is hard to match on Windows.
35 #define LOGGING_TIMESTAMP_REGEX "^20[0-9][0-9]-[01][0-9]-[0-3][0-9]T[0-2][0-9]:[0-5][0-9]:[0-5][0-9]"
36 
37 
38 typedef enum
39 {
49 
50 
51 /**
52  * Enum used as a parameter in LogDebug(), used to print even more detailed
53  * info than Log(LOG_LEVEL_DEBUG).
54  */
56 {
57  LOG_MOD_NONE = 0, /* DEFAULT DEBUG LOGGING */
58  LOG_MOD_EVALCTX, /* evaluator */
59  LOG_MOD_EXPAND, /* variables expansion */
60  LOG_MOD_ITERATIONS, /* evaluator iteration engine */
61  LOG_MOD_PARSER, /* yacc */
62  LOG_MOD_VARTABLE, /* variables tables */
63  LOG_MOD_VARS, /* variables promise */
64  LOG_MOD_LOCKS, /* locks.c */
65  LOG_MOD_PS, /* ps parsing */
67 };
68 
69 
70 #include <logging_priv.h>
71 
72 
73 typedef struct
74 {
77  bool color;
78 
81 
82 const char *LogLevelToString(LogLevel level);
83 LogLevel LogLevelFromString(const char *level);
84 
85 /**
86  * @brief Return the standard timestamp format used in logging.
87  * @param dest Output buffer
88  * @param n size of output buffer
89  * @param timestamp Timespec to format
90  * @return True if successful, otherwise "<unknown>" will be printed to buffer
91  */
92 bool LoggingFormatTimestamp(char dest[64], size_t n, struct tm *timestamp);
93 
96 
97 /**
98  * Whether a message with level #level would be logged by Log() or not.
99  */
100 bool WouldLog(LogLevel level);
101 
102 void Log(LogLevel level, const char *fmt, ...) FUNC_ATTR_PRINTF(2, 3);
103 void LogDebug(enum LogModule mod, const char *fmt, ...) FUNC_ATTR_PRINTF(2, 3);
104 void LogRaw(LogLevel level, const char *prefix, const void *buf, size_t buflen);
105 void VLog(LogLevel level, const char *fmt, va_list ap);
106 
107 void LoggingSetAgentType(const char *type);
108 void LoggingEnableTimestamps(bool enable);
109 void LogSetGlobalLevel(LogLevel level);
110 void LogSetGlobalLevelArgOrExit(const char *const arg);
112 
113 void LoggingSetColor(bool enabled);
114 
115 /*
116  * Portable syslog()
117  */
118 void LogToSystemLog(const char *msg, LogLevel level);
119 
120 /*
121  * Portable strerror(errno)
122  */
123 const char *GetErrorStr(void);
124 const char *GetErrorStrFromCode(int error_code);
125 
126 void LogModuleHelp(void);
127 bool LogModuleEnabled(enum LogModule mod);
128 void LogEnableModule(enum LogModule mod);
129 bool LogEnableModulesFromString(char *s);
130 
131 // byte_magnitude and byte_unit are used to print readable byte counts
132 long byte_magnitude(long bytes);
133 const char *byte_unit(long bytes);
134 
135 #endif
#define FUNC_ATTR_PRINTF(string_index, first_to_check)
Definition: compiler.h:52
#define const
Definition: getopt.c:38
LogLevel
Definition: log.h:30
void LogRaw(LogLevel level, const char *prefix, const void *buf, size_t buflen)
Logs binary data in #buf, with unprintable bytes translated to '.'. Message is prefixed with #prefix.
Definition: logging.c:382
const char * byte_unit(long bytes)
Definition: logging.c:617
void LoggingSetAgentType(const char *type)
Definition: logging.c:83
bool LogEnableModulesFromString(char *s)
Definition: logging.c:476
void VLog(LogLevel level, const char *fmt, va_list ap)
Definition: logging.c:322
void LogSetGlobalLevelArgOrExit(const char *const arg)
Definition: logging.c:567
void LoggingEnableTimestamps(bool enable)
Definition: logging.c:88
const char * GetErrorStrFromCode(int error_code)
Definition: logging.c:270
void LoggingSetColor(bool enabled)
Definition: logging.c:586
void LogModuleHelp(void)
Definition: logging.c:453
LogModule
Definition: logging.h:56
@ LOG_MOD_ITERATIONS
Definition: logging.h:60
@ LOG_MOD_NONE
Definition: logging.h:57
@ LOG_MOD_LOCKS
Definition: logging.h:64
@ LOG_MOD_VARTABLE
Definition: logging.h:62
@ LOG_MOD_VARS
Definition: logging.h:63
@ LOG_MOD_PARSER
Definition: logging.h:61
@ LOG_MOD_PS
Definition: logging.h:65
@ LOG_MOD_MAX
Definition: logging.h:66
@ LOG_MOD_EVALCTX
Definition: logging.h:58
@ LOG_MOD_EXPAND
Definition: logging.h:59
LoggingContext * GetCurrentThreadContext(void)
Definition: logging.c:56
long byte_magnitude(long bytes)
Definition: logging.c:593
void LogSetGlobalLevel(LogLevel level)
Definition: logging.c:561
void LogDebug(enum LogModule mod, const char *fmt,...)
Definition: logging.c:542
LogLevel
Definition: logging.h:39
@ LOG_LEVEL_ERR
Definition: logging.h:42
@ LOG_LEVEL_NOTICE
Definition: logging.h:44
@ LOG_LEVEL_DEBUG
Definition: logging.h:47
@ LOG_LEVEL_WARNING
Definition: logging.h:43
@ LOG_LEVEL_CRIT
Definition: logging.h:41
@ LOG_LEVEL_VERBOSE
Definition: logging.h:46
@ LOG_LEVEL_NOTHING
Definition: logging.h:40
@ LOG_LEVEL_INFO
Definition: logging.h:45
bool LoggingFormatTimestamp(char dest[64], size_t n, struct tm *timestamp)
Return the standard timestamp format used in logging.
Definition: logging.c:200
void LogEnableModule(enum LogModule mod)
Definition: logging.c:446
LogLevel LogGetGlobalLevel(void)
Definition: logging.c:581
void LoggingFreeCurrentThreadContext(void)
Definition: logging.c:70
LogLevel LogLevelFromString(const char *level)
Definition: logging.c:135
void LogToSystemLog(const char *msg, LogLevel level)
Definition: logging.c:261
bool LogModuleEnabled(enum LogModule mod)
Definition: logging.c:527
bool WouldLog(LogLevel level)
Definition: logging.c:308
const char * GetErrorStr(void)
Definition: logging.c:275
void Log(LogLevel level, const char *fmt,...)
Definition: logging.c:409
const char * LogLevelToString(LogLevel level)
Definition: logging.c:112
bool color
Definition: logging.h:77
LoggingPrivContext * pctx
Definition: logging.h:79
LogLevel log_level
Definition: logging.h:75
LogLevel report_level
Definition: logging.h:76