-/*
- * Copyright (c) 2016, Olivier MATZ <zer0@droids-corp.org>
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- *
- * * Redistributions of source code must retain the above copyright
- * notice, this list of conditions and the following disclaimer.
- * * Redistributions in binary form must reproduce the above copyright
- * notice, this list of conditions and the following disclaimer in the
- * documentation and/or other materials provided with the distribution.
- * * Neither the name of the University of California, Berkeley nor the
- * names of its contributors may be used to endorse or promote products
- * derived from this software without specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
- * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
- * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
- * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+/* SPDX-License-Identifier: BSD-3-Clause
+ * Copyright 2016, Olivier MATZ <zer0@droids-corp.org>
*/
#define _GNU_SOURCE /* for vasprintf */
#include <stdlib.h>
#include <string.h>
#include <errno.h>
+#include <syslog.h>
#include <ecoli_malloc.h>
#include <ecoli_string.h>
#include <ecoli_log.h>
+#include <ecoli_test.h>
+
+EC_LOG_TYPE_REGISTER(log);
static ec_log_t ec_log_fct = ec_log_default_cb;
static void *ec_log_opaque;
int ec_log_level_set(enum ec_log_level level)
{
- if (level < 0 || level > EC_LOG_DEBUG)
+ if (level > EC_LOG_DEBUG)
return -1;
global_level = level;
int ec_log_fct_register(ec_log_t usr_log, void *opaque)
{
- errno = 0;
-
if (usr_log == NULL) {
ec_log_fct = ec_log_default_cb;
ec_log_opaque = NULL;
return ret;
}
+
+/* LCOV_EXCL_START */
+static int
+log_cb(int type, enum ec_log_level level, void *opaque, const char *str)
+{
+ (void)type;
+ (void)level;
+ (void)str;
+ *(int *)opaque = 1;
+
+ return 0;
+}
+
+static int ec_log_testcase(void)
+{
+ ec_log_t prev_log_cb;
+ void *prev_opaque;
+ const char *logname;
+ int testres = 0;
+ int check_cb = 0;
+ int logtype;
+ int level;
+ int ret;
+
+ prev_log_cb = ec_log_fct;
+ prev_opaque = ec_log_opaque;
+
+ ret = ec_log_fct_register(log_cb, &check_cb);
+ testres |= EC_TEST_CHECK(ret == 0,
+ "cannot register log function\n");
+ EC_LOG(LOG_ERR, "test\n");
+ testres |= EC_TEST_CHECK(check_cb == 1,
+ "log callback was not invoked\n");
+ logtype = ec_log_lookup("dsdedesdes");
+ testres |= EC_TEST_CHECK(logtype == -1,
+ "lookup invalid name should return -1");
+ logtype = ec_log_lookup("log");
+ logname = ec_log_name(logtype);
+ testres |= EC_TEST_CHECK(logname != NULL &&
+ !strcmp(logname, "log"),
+ "cannot get log name\n");
+ logname = ec_log_name(-1);
+ testres |= EC_TEST_CHECK(logname != NULL &&
+ !strcmp(logname, "unknown"),
+ "cannot get invalid log name\n");
+ logname = ec_log_name(34324);
+ testres |= EC_TEST_CHECK(logname != NULL &&
+ !strcmp(logname, "unknown"),
+ "cannot get invalid log name\n");
+ level = ec_log_level_get();
+ ret = ec_log_level_set(2);
+ testres |= EC_TEST_CHECK(ret == 0 && ec_log_level_get() == 2,
+ "cannot set log level\n");
+ ret = ec_log_level_set(10);
+ testres |= EC_TEST_CHECK(ret != 0,
+ "should not be able to set log level\n");
+
+ ret = ec_log_fct_register(NULL, NULL);
+ ec_log_level_set(LOG_DEBUG);
+ EC_LOG(LOG_DEBUG, "test log\n");
+ ec_log_level_set(LOG_INFO);
+ EC_LOG(LOG_DEBUG, "test log (not displayed)\n");
+ ec_log_level_set(level);
+
+ ec_log_fct = prev_log_cb;
+ ec_log_opaque = prev_opaque;
+
+ return testres;
+}
+/* LCOV_EXCL_STOP */
+
+static struct ec_test ec_log_test = {
+ .name = "log",
+ .test = ec_log_testcase,
+};
+
+EC_TEST_REGISTER(ec_log_test);