level_list.c 3.3 KB
Newer Older
Hardy Simpson's avatar
Hardy Simpson committed
1 2 3 4 5
/*
 * This file is part of the zlog Library.
 *
 * Copyright (C) 2011 by Hardy Simpson <HardySimpson1984@gmail.com>
 *
6
 * Licensed under the LGPL v2.1, see the file COPYING in base directory.
Hardy Simpson's avatar
Hardy Simpson committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
 */

#include <stdio.h>
#include <ctype.h>
#include <errno.h>
#include "syslog.h"

#include "zc_defs.h"
#include "level.h"
#include "level_list.h"

/* zlog_level_list == zc_arraylist_t<zlog_level_t> */

void zlog_level_list_profile(zc_arraylist_t *levels, int flag)
{
	int i;
	zlog_level_t *a_level;

	zc_assert(levels,);
	zc_profile(flag, "--level_list[%p]--", levels);
	zc_arraylist_foreach(levels, i, a_level) {
28 29
		/* skip empty slots */
		if (a_level) zlog_level_profile(a_level, flag);
Hardy Simpson's avatar
Hardy Simpson committed
30 31 32 33 34 35 36 37 38
	}
	return;
}

/*******************************************************************************/
void zlog_level_list_del(zc_arraylist_t *levels)
{
	zc_assert(levels,);
	zc_arraylist_del(levels);
39
	zc_debug("zc_level_list_del[%p]", levels);
Hardy Simpson's avatar
Hardy Simpson committed
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
	return;
}

static int zlog_level_list_set_default(zc_arraylist_t *levels)
{
	return zlog_level_list_set(levels, "* = 0, LOG_INFO")
	|| zlog_level_list_set(levels, "DEBUG = 20, LOG_DEBUG")
	|| zlog_level_list_set(levels, "INFO = 40, LOG_INFO")
	|| zlog_level_list_set(levels, "NOTICE = 60, LOG_NOTICE")
	|| zlog_level_list_set(levels, "WARN = 80, LOG_WARNING")
	|| zlog_level_list_set(levels, "ERROR = 100, LOG_ERR")
	|| zlog_level_list_set(levels, "FATAL = 120, LOG_ALERT")
	|| zlog_level_list_set(levels, "UNKNOWN = 254, LOG_ERR")
	|| zlog_level_list_set(levels, "! = 255, LOG_INFO");
}

56
zc_arraylist_t *zlog_level_list_new(void)
Hardy Simpson's avatar
Hardy Simpson committed
57 58 59 60 61 62 63 64 65
{
	zc_arraylist_t *levels;

	levels = zc_arraylist_new((zc_arraylist_del_fn)zlog_level_del);
	if (!levels) {
		zc_error("zc_arraylist_new fail");
		return NULL;
	}

Hardy Simpson's avatar
Hardy Simpson committed
66
	if (zlog_level_list_set_default(levels)) {
Hardy Simpson's avatar
Hardy Simpson committed
67
		zc_error("zlog_level_set_default fail");
Hardy Simpson's avatar
Hardy Simpson committed
68
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
69 70
	}

71
	//zlog_level_list_profile(levels, ZC_DEBUG);
Hardy Simpson's avatar
Hardy Simpson committed
72 73 74 75
	return levels;
err:
	zc_arraylist_del(levels);
	return NULL;
Hardy Simpson's avatar
Hardy Simpson committed
76 77 78 79 80 81 82 83 84 85 86 87 88
}

/*******************************************************************************/
int zlog_level_list_set(zc_arraylist_t *levels, char *line)
{
	zlog_level_t *a_level;

	a_level = zlog_level_new(line);
	if (!a_level) {
		zc_error("zlog_level_new fail");
		return -1;
	}

Hardy Simpson's avatar
Hardy Simpson committed
89
	if (zc_arraylist_set(levels, a_level->int_level, a_level)) {
Hardy Simpson's avatar
Hardy Simpson committed
90
		zc_error("zc_arraylist_set fail");
Hardy Simpson's avatar
Hardy Simpson committed
91
		goto err;
Hardy Simpson's avatar
Hardy Simpson committed
92 93
	}

Hardy Simpson's avatar
Hardy Simpson committed
94 95 96 97 98
	return 0;
err:
	zc_error("line[%s]", line);
	zlog_level_del(a_level);
	return -1;
Hardy Simpson's avatar
Hardy Simpson committed
99 100 101 102 103 104
}

zlog_level_t *zlog_level_list_get(zc_arraylist_t *levels, int l)
{
	zlog_level_t *a_level;

105
#if 0
Hardy Simpson's avatar
Hardy Simpson committed
106 107 108 109 110
	if ((l <= 0) || (l > 254)) {
		/* illegal input from zlog() */
		zc_error("l[%d] not in (0,254), set to UNKOWN", l);
		l = 254;
	}
111
#endif
Hardy Simpson's avatar
Hardy Simpson committed
112 113

	a_level = zc_arraylist_get(levels, l);
Hardy Simpson's avatar
Hardy Simpson committed
114 115 116
	if (a_level) {
		return a_level;
	} else {
Hardy Simpson's avatar
Hardy Simpson committed
117
		/* empty slot */
118
		zc_error("l[%d] not in (0,254), or has no level defined,"
Hardy Simpson's avatar
Hardy Simpson committed
119
			"see configure file define, set to UNKOWN", l);
Hardy Simpson's avatar
Hardy Simpson committed
120
		return zc_arraylist_get(levels, 254);
Hardy Simpson's avatar
Hardy Simpson committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
	}
}

/*******************************************************************************/

int zlog_level_list_atoi(zc_arraylist_t *levels, char *str)
{
	int i;
	zlog_level_t *a_level;

	if (str == NULL || *str == '\0') {
		zc_error("str is [%s], can't find level", str);
		return -1;
	}

	zc_arraylist_foreach(levels, i, a_level) {
		if (a_level && STRICMP(str, ==, a_level->str_uppercase)) {
			return i;
		}
	}

	zc_error("str[%s] can't found in level list", str);
	return -1;
}