File: log.h

package info (click to toggle)
ndctl 82-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,460 kB
  • sloc: ansic: 42,027; sh: 3,974; makefile: 28
file content (69 lines) | stat: -rw-r--r-- 2,463 bytes parent folder | download | duplicates (3)
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
/* SPDX-License-Identifier: LGPL-2.1 */
/* Copyright (C) 2016-2020, Intel Corporation. All rights reserved. */
#ifndef __UTIL_LOG_H__
#define __UTIL_LOG_H__
#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>

struct log_ctx;
typedef void (*log_fn)(struct log_ctx *ctx, int priority, const char *file,
		int line, const char *fn, const char *format, va_list args);

struct log_ctx {
	log_fn log_fn;
	const char *owner;
	int log_priority;
	FILE *log_file;
};

void log_syslog(struct log_ctx *ctx, int priority, const char *file, int line,
		const char *fn, const char *format, va_list args);
void log_standard(struct log_ctx *ctx, int priority, const char *file, int line,
		  const char *fn, const char *format, va_list args);
void log_file(struct log_ctx *ctx, int priority, const char *file, int line,
	      const char *fn, const char *format, va_list args);
void do_log(struct log_ctx *ctx, int priority, const char *file, int line,
		const char *fn, const char *format, ...)
	__attribute__((format(printf, 6, 7)));
void log_init(struct log_ctx *ctx, const char *owner, const char *log_env);
static inline void __attribute__((always_inline, format(printf, 2, 3)))
	log_null(struct log_ctx *ctx, const char *format, ...) {}

#define log_cond(ctx, prio, arg...) \
do { \
	if ((ctx)->log_priority >= prio) \
		do_log(ctx, prio, __FILE__, __LINE__, __FUNCTION__, ## arg); \
} while (0)

#ifdef ENABLE_LOGGING
#  ifdef ENABLE_DEBUG
#    define log_dbg(ctx, arg...) log_cond(ctx, LOG_DEBUG, ## arg)
#  else
#    define log_dbg(ctx, arg...) log_null(ctx, ## arg)
#  endif
#  define log_info(ctx, arg...) log_cond(ctx, LOG_INFO, ## arg)
#  define log_err(ctx, arg...) log_cond(ctx, LOG_ERR, ## arg)
#  define log_notice(ctx, arg...) log_cond(ctx, LOG_NOTICE, ## arg)
#else
#  define log_dbg(ctx, arg...) log_null(ctx, ## arg)
#  define log_info(ctx, arg...) log_null(ctx, ## arg)
#  define log_err(ctx, arg...) log_null(ctx, ## arg)
#  define log_notice(ctx, arg...) log_null(ctx, ## arg)
#endif

#define dbg(x, arg...) log_dbg(&(x)->ctx, ## arg)
#define info(x, arg...) log_info(&(x)->ctx, ## arg)
#define err(x, arg...) log_err(&(x)->ctx, ## arg)
#define notice(x, arg...) log_notice(&(x)->ctx, ## arg)

#ifndef HAVE_SECURE_GETENV
#  ifdef HAVE___SECURE_GETENV
#    define secure_getenv __secure_getenv
#  else
#    warning neither secure_getenv nor __secure_getenv is available.
#    define secure_getenv getenv
#  endif
#endif

#endif /* __UTIL_LOG_H__ */