[go: up one dir, main page]

File: util_log.c

package info (click to toggle)
s390-tools 2.35.0-1
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 12,220 kB
  • sloc: ansic: 184,236; sh: 12,152; cpp: 4,954; makefile: 2,763; perl: 2,519; asm: 1,085; python: 697; xml: 29
file content (70 lines) | stat: -rw-r--r-- 1,539 bytes parent folder | download | duplicates (2)
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
/*
 * Multi-level message logging
 *
 * Copyright IBM Corp. 2021
 *
 * s390-tools is free software; you can redistribute it and/or modify
 * it under the terms of the MIT license. See LICENSE for details.
 */

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

#include "lib/util_log.h"

static int current_log_level = UTIL_LOG_INFO;

static const char *log_level_to_str(int log_level)
{
	switch (log_level) {
	case UTIL_LOG_ERROR:
		return "ERROR";
	case UTIL_LOG_WARN:
		return "WARN";
	case UTIL_LOG_INFO:
		return "INFO";
	case UTIL_LOG_DEBUG:
		return "DEBUG";
	case UTIL_LOG_TRACE:
		return "TRACE";
	default:
		return "UNKNW";
	}
}

static void log_helper(FILE *fp, int log_level, const char *fmt, va_list args)
{
	if (current_log_level < log_level)
		return;

	fprintf(fp, "%5s: ", log_level_to_str(log_level));
	vfprintf(fp, fmt, args);
}

/**
 * Changes the current log level
 *
 * @param[in] log_level A new log level to be set as the current one
 */
void util_log_set_level(int log_level)
{
	current_log_level = log_level;
}

/**
 * Outputs the given message on stderr
 *
 * The given message is printed only if the current log level is >= than the given one.
 *
 * @param[in] log_level Log level starting from which the given message shall be printed
 * @param[in] fmt       Format string for generation of the log message
 * @param[in] ...       Parameters for format string
 */
void util_log_print(int log_level, const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	log_helper(stderr, log_level, fmt, args);
	va_end(args);
}