Skip to content

Commit d8c1025

Browse files
authored
Merge pull request #123 from gringolito/master
Added API to see if a given level is enabled
2 parents f1ad1d8 + 33dd60f commit d8c1025

6 files changed

Lines changed: 108 additions & 3 deletions

File tree

src/zlog.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,5 +1023,10 @@ int zlog_set_record(const char *rname, zlog_record_fn record_output)
10231023
}
10241024
return rc;
10251025
}
1026+
/*******************************************************************************/
1027+
int zlog_level_enabled(zlog_category_t *category, const int level)
1028+
{
1029+
return category && (zlog_category_needless_level(category, level) == 0);
1030+
}
10261031

10271032
const char *zlog_version(void) { return ZLOG_VERSION; }

src/zlog.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ void zlog_fini(void);
3131
void zlog_profile(void);
3232

3333
zlog_category_t *zlog_get_category(const char *cname);
34+
int zlog_level_enabled(zlog_category_t *category, const int level);
3435

3536
int zlog_put_mdc(const char *key, const char *value);
3637
char *zlog_get_mdc(const char *key);
@@ -263,6 +264,14 @@ typedef enum {
263264
hdzlog(__FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
264265
ZLOG_LEVEL_DEBUG, buf, buf_len)
265266

267+
/* enabled macros */
268+
#define zlog_fatal_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_FATAL)
269+
#define zlog_error_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_ERROR)
270+
#define zlog_warn_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_WARN)
271+
#define zlog_notice_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_NOTICE)
272+
#define zlog_info_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_INFO)
273+
#define zlog_debug_enabled(zc) zlog_level_enabled(zc, ZLOG_LEVEL_DEBUG)
274+
266275
#ifdef __cplusplus
267276
}
268277
#endif

test/makefile

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ exe = \
2020
test_press_write2 \
2121
test_press_syslog \
2222
test_syslog \
23-
test_default \
24-
test_profile \
25-
test_category
23+
test_default \
24+
test_profile \
25+
test_category \
26+
test_enabled
2627

2728
all : $(exe)
2829

test/test_enabled.c

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* This file is part of the zlog Library.
3+
*
4+
* Copyright (C) 2018 by Teracom Telemática S/A
5+
*
6+
* Licensed under the LGPL v2.1, see the file COPYING in base directory.
7+
*/
8+
9+
#include <stdio.h>
10+
#include "test_enabled.h"
11+
12+
int main(int argc, char** argv)
13+
{
14+
int rc;
15+
zlog_category_t *zc;
16+
17+
rc = zlog_init("test_enabled.conf");
18+
if (rc) {
19+
printf("init failed\n");
20+
return -1;
21+
}
22+
23+
zc = zlog_get_category("my_cat");
24+
if (!zc) {
25+
printf("get cat fail\n");
26+
zlog_fini();
27+
return -2;
28+
}
29+
30+
if (zlog_trace_enabled(zc)) {
31+
/* do something heavy to collect data */
32+
zlog_trace(zc, "hello, zlog - trace");
33+
}
34+
35+
if (zlog_debug_enabled(zc)) {
36+
/* do something heavy to collect data */
37+
zlog_debug(zc, "hello, zlog - debug");
38+
}
39+
40+
if (zlog_info_enabled(zc)) {
41+
/* do something heavy to collect data */
42+
zlog_info(zc, "hello, zlog - info");
43+
}
44+
45+
zlog_fini();
46+
47+
return 0;
48+
}

test/test_enabled.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
[global]
2+
default format = "%V %v %m%n"
3+
[levels]
4+
TRACE = 30, LOG_DEBUG
5+
[rules]
6+
my_cat.TRACE >stdout;

test/test_enabled.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* This file is part of the zlog Library.
3+
*
4+
* Copyright (C) 2018 by Teracom Telemática S/A
5+
*
6+
* The zlog Library is free software: you can redistribute it and/or modify
7+
* it under the terms of the GNU Lesser General Public License as published by
8+
* the Free Software Foundation, either version 3 of the License, or
9+
* (at your option) any later version.
10+
*
11+
* The zlog Library is distributed in the hope that it will be useful,
12+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+
* GNU Lesser General Public License for more details.
15+
*
16+
* You should have received a copy of the GNU Lesser General Public License
17+
* along with the zlog Library. If not, see <http://www.gnu.org/licenses/>.
18+
*/
19+
20+
#ifndef __test_level_h
21+
#define __test_level_h
22+
23+
#include "zlog.h"
24+
25+
enum {
26+
ZLOG_LEVEL_TRACE = 30,
27+
/* must equals conf file setting */
28+
};
29+
30+
#define zlog_trace(cat, format, args...) \
31+
zlog(cat, __FILE__, sizeof(__FILE__)-1, __func__, sizeof(__func__)-1, __LINE__, \
32+
ZLOG_LEVEL_TRACE, format, ##args)
33+
34+
#define zlog_trace_enabled(cat) zlog_level_enabled(cat, ZLOG_LEVEL_TRACE)
35+
36+
#endif

0 commit comments

Comments
 (0)