1
1

Initial dump from Zyxel

This commit is contained in:
2026-04-17 17:00:52 +02:00
commit 81fec250f4
23116 changed files with 5231237 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
Makefile-1.0
+94
View File
@@ -0,0 +1,94 @@
#
# OpenWRT makefile
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#
include $(TOPDIR)/rules.mk
#
# package information
#
PKG_NAME := libzlog
PKG_VERSION := 1.0
PKG_RELEASE := 1
PKG_NAME_VER := $(PKG_NAME)-$(PKG_VERSION)
PKG_SRC_DIR := $(ZYXEL_PUBLIC_PACKAGE)/$(PKG_NAME)/$(PKG_NAME_VER)
PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME_VER)
#
# Must be after package information
# Otherwise, the above variables will be incorrect
#
include $(INCLUDE_DIR)/package.mk
#zlog
export CONFIG_ZLOG_USE_DEBUG
#
# Package information
#
define Package/$(PKG_NAME)
SECTION := net
CATEGORY := Zyxel public package
TITLE := log
DEFAULT := y
endef
#
# Clear the build dir
#
define Build/Clean
rm -rf $(PKG_BUILD_DIR)
endef
#
# Prepare the package
# 1. create the same dir including sub-dir under build dir
# 2. link files to the same dir and sub-dir
#
define Build/Prepare
rm -rf $(PKG_BUILD_DIR)
$$(call link_files,$(PKG_NAME_VER),$(BUILD_DIR))
endef
#
# Compile the build dir
#
define Package/$(PKG_NAME)/compile
$(MAKE) -C $(PKG_BUILD_DIR)
endef
#
# Install the package to be accessed by other later-compiled packages
# 1. copy header files to staging dir
# 2. copy library to staging dir
#
define Build/InstallDev
$(INSTALL_DIR) $(1)/usr/include
$(CP) $(PKG_BUILD_DIR)/*.h $(1)/usr/include/
$(INSTALL_DIR) $(1)/usr/lib
$(CP) $(PKG_BUILD_DIR)/$(PKG_NAME).so $(1)/usr/lib/
endef
#
# Disable Dependencies Check
#
define CheckDependencies
endef
#
# Install the package to linux file system
#
define Package/$(PKG_NAME)/install
$(INSTALL_DIR) $(1)/lib
$(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME).so $(1)/lib
$(INSTALL_DIR) $(1)/usr/sbin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/zlog $(1)/usr/sbin
# $(INSTALL_DIR) $(1)/lib/public
# $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME).so $(1)/lib/public
# ln -s /lib/public/$(PKG_NAME).so $(1)/lib/$(PKG_NAME).so
endef
$(eval $(call BuildPackage,$(PKG_NAME)))
+41
View File
@@ -0,0 +1,41 @@
#
# source code makefile
#
LIB_NAME = libzlog
OBJS = zlog_api.o
CURRENT_DIR = $(shell pwd)
#LINK_LIB = -lpthread
#LIBS_PATH =
LDFLAGS += $(LIBS_PATH) $(LINK_LIB)
#zlog
ifeq ($(CONFIG_ZLOG_USE_DEBUG),y)
CFLAGS += -DCONFIG_ZLOG_USE_DEBUG
endif
#CFLAGS += -I$(TOOLCHAIN)/include -I. -std=gnu99
CFLAGS += -I. -std=gnu99 -Wall
APPS=zlog
APPS_SRC=zlog.c
.PHONY : $(LIB_NAME)
all: clean $(LIB_NAME) $(APPS)
.c.o:
@$(CC) $(CFLAGS) -Wall -Werror -fPIC -c $< -o $@
$(LIB_NAME) : $(OBJS)
@$(CC) $(LDFLAGS) -shared -o $(LIB_NAME).so $(OBJS)
$(APPS) : $(APPS_SRC)
@$(CC) $(CFLAGS) $^ $(LDFLAGS) -L. -lzlog -o $@
clean:
rm -rf *.o
rm -rf *.so
rm -rf $(APPS)
+101
View File
@@ -0,0 +1,101 @@
/*!
* @file zlog.c
* application to generate zlog
*
* @author Horace Chang
* @date 2018-10-24 18:38:46
* @copyright Copyright 2018 Zyxel Communications Corp. All Rights Reserved.
*/
//==============================================================================
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <sys/types.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdint.h>
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h> // for syscall()
#include <sys/syscall.h> // for syscall()
#include "zlog_api.h"
#include <errno.h> // for program_invocation_short_name
#include "zcfg_debug.h"
#define TYPE_SYSLOG 1
#define TYPE_ZCFGLOG_PREFIX 2
int priority = LOG_INFO;
char category[64]="";
int debug = 0;
int type = TYPE_SYSLOG;
void
print_usage(void )
{
printf("usage [-d] [-p priority] -c category log message\n");
// printf(" type: syslog (default), zcfgLogPrefix \n");
}
int
get_input ( int argc, char *argv[] )
{
int opt;
while ((opt = getopt(argc, argv, "hdp:c:f:t:")) != -1) {
// printf("%s: %c\n",__FUNCTION__,opt);
switch (opt) {
case 'h':
print_usage();
break;
case 'p':
priority = atoi(optarg);
break;
case 'c':
snprintf(category, sizeof(category),"%s",optarg);
break;
case 'd':
debug = 1;
printf("debug = %d\n",debug);
break;
case 't':
if ( strcmp("zcfgLogPrefix",optarg ) == 0 ) {
type = TYPE_ZCFGLOG_PREFIX;
}
break;
default: /* '?' */
exit(EXIT_FAILURE);
}
}
return 0;
}
int
main( int argc, char *argv[] )
{
int lens = 0;
char rawbuf[512];
if ( argc < 1 ) {
print_usage();
exit(EXIT_FAILURE);
}
get_input ( argc, argv );
for (; optind < argc; optind++) {
lens += snprintf (rawbuf + lens, sizeof(rawbuf)-lens,"%s ", argv[optind]);
rawbuf[lens - 1] = '\n';
}
if ( debug ) printf("%d, %s %s, type=%d\n",priority,category, rawbuf,type);
if ( type == TYPE_SYSLOG ) {
syslog( priority, "%s %s",category, rawbuf);
}
return 0;
}
+386
View File
@@ -0,0 +1,386 @@
/*!
* @file zlog_api.c
* library of syslog-ng
*
* @author CP Wang
* @date 2017-12-12 18:38:46
* @copyright Copyright 2017 Zyxel Communications Corp. All Rights Reserved.
*/
//==============================================================================
#include <stdio.h>
#include <string.h>
#include <pthread.h>
#include <time.h>
#include <sys/types.h>
#include <syslog.h>
#include <stdarg.h>
#include <stdint.h>
#define _GNU_SOURCE /* See feature_test_macros(7) */
#include <unistd.h> // for syscall()
#include <sys/syscall.h> // for syscall()
#include "zlog_api.h"
#include <errno.h> // for program_invocation_short_name
extern char *program_invocation_short_name;
//==============================================================================
/*
0 - printf()
1 - syslog-ng
*/
#ifdef CONFIG_ZLOG_USE_DEBUG
#define _USE_SYSLOG 1
#else
#define _USE_SYSLOG 0
#endif
/*
--- IMPORTANT ---
this macro is to break the circular dependency with zos
this should always be the same as zos_pid_get()
*/
#define _pid_get() (uint32_t)(syscall(__NR_gettid))
#define _dbg_printf(_fmt_, ...) \
fprintf(stderr, "[PID %u] %s line %d, %s(), " _fmt_, _pid_get(), __FILE__, __LINE__, __FUNCTION__, ##__VA_ARGS__)
//==============================================================================
static zlog_level_t _log_level = ZLOG_LEVEL_INFO;
static zlog_faci_t _facility = ZLOG_FACI_USER;
//==============================================================================
#if 0
static const char *_faci_str(
zlog_faci_t facility
)
{
switch (facility)
{
case ZLOG_FACI_USER: return "user";
case ZLOG_FACI_KERNEL: return "kernel";
default: return "unknown";
}
}
#endif
static const char *_level_str(
zlog_level_t level
)
{
switch (level)
{
case ZLOG_LEVEL_EMERG: return "EMERG";
case ZLOG_LEVEL_ALERT: return "ALERT";
case ZLOG_LEVEL_CRITICAL: return "CRITICAL";
case ZLOG_LEVEL_ERROR: return "ERROR";
case ZLOG_LEVEL_WARNING: return "WARNING";
case ZLOG_LEVEL_NOTICE: return "NOTICE";
case ZLOG_LEVEL_INFO: return "INFO";
case ZLOG_LEVEL_DEBUG: return "DEBUG";
default: return "UNKNOWN";
}
}
/*!
* get string of date time
*
* @param [out] str string buffer to get date time.
* The buffer size must be >= 20.
*
* @return true - successful
* false - failed
*/
static bool _datetime_str(
char *str
)
{
int r;
time_t now = time(0);
struct tm result;
struct tm* ltm = localtime_r(&now, &result);
if (ltm == NULL)
{
return false;
}
r = sprintf(str, "%04d-%02d-%02d %02d:%02d:%02d", 1900 + ltm->tm_year, 1 + ltm->tm_mon,
ltm->tm_mday, ltm->tm_hour, ltm->tm_min, ltm->tm_sec);
if (r <= 0)
{
return false;
}
return true;
}
//==============================================================================
/*!
* opens a connection to the system logger for a program
*
* @param [in] module_name module name. If NULL, the program name is used.
* @param [in] facility log facility
*
* @return true - successful
* false - failed
*/
bool zlog_open(
char *module,
zlog_faci_t facility
)
{
int ng_facility;
switch (facility)
{
case ZLOG_FACI_USER:
ng_facility = LOG_USER;
break;
case ZLOG_FACI_KERNEL:
ng_facility = LOG_KERN;
break;
default:
_dbg_printf("ERROR : invalid facility %d\n", facility);
return false;
}
_facility = facility;
#if 1
openlog(module, LOG_NOWAIT | LOG_NDELAY, ng_facility);
#else
/* output to console */
openlog(module, LOG_NOWAIT | LOG_NDELAY | LOG_PERROR, ng_facility);
#endif
return true;
}
/*!
* generates a log message
*
* @param [in] level log level
* @param [in] file C file name
* @param [in] line line of the code
* @param [in] func API name
* @param [in] format string format of log message
* @param [in] ... any arguments required by the format
*
* @note no return value, reasons are
* 1. nothing can do it fail
* 2. if fail, the error message will pop up automatically
*/
void zlog_log_f(
const zlog_level_t level,
const char *file,
const int line,
const char *func,
const char *format,
...
)
{
#define _STR_MAX_LEN 2048
#define _MSG_MAX_LEN (_STR_MAX_LEN * 2)
char str[_STR_MAX_LEN + 1] = {0};
char msg[_MSG_MAX_LEN + 1] = {0};
char date[20] = {0};
int r;
va_list ap;
memset(str, 0, sizeof(str));
memset(msg, 0, sizeof(msg));
memset(date, 0, sizeof(date));
#if _USE_SYSLOG
int priority;
switch (level)
{
case ZLOG_LEVEL_EMERG:
priority = LOG_EMERG;
break;
case ZLOG_LEVEL_ALERT:
priority = LOG_ALERT;
break;
case ZLOG_LEVEL_CRITICAL:
priority = LOG_CRIT;
break;
case ZLOG_LEVEL_ERROR:
priority = LOG_ERR;
break;
case ZLOG_LEVEL_WARNING:
priority = LOG_WARNING;
break;
case ZLOG_LEVEL_NOTICE:
priority = LOG_NOTICE;
break;
case ZLOG_LEVEL_INFO:
priority = LOG_INFO;
break;
case ZLOG_LEVEL_DEBUG:
priority = LOG_DEBUG;
break;
default:
_dbg_printf("ERROR : invalid level %d\n", level);
return ;
}
#else // _USE_SYSLOG
if (level < 0 || level > ZLOG_LEVEL_DEBUG)
{
_dbg_printf("ERROR : invalid level %d\n", level);
return;
}
#endif // _USE_SYSLOG
if (_datetime_str(date) == false)
{
_dbg_printf("fail to get date time\n");
return;
}
if (format == NULL)
{
_dbg_printf("format == NULL\n");
return;
}
va_start(ap, format);
r = vsnprintf(str, _STR_MAX_LEN, format, ap);
va_end(ap);
if (r < 0 || r > _STR_MAX_LEN)
{
_dbg_printf("fail to vsnprintf()\n");
return;
}
// remove tailing '\n'
r = strlen(str);
if (r > 0 && r <= _STR_MAX_LEN)
{
if (str[r - 1] == '\n' || str[r - 1] == '\r')
{
str[r - 1] = 0;
}
}
if (file)
{
/*
<time> <program><<pid>, <file>:<line> <func> <severity> : <message>
*/
r = snprintf(msg, _MSG_MAX_LEN, "%s %s<%u>, %s:%d, %s(), %s: %s\n", date,
program_invocation_short_name, _pid_get(), file,
line, func, _level_str(level), str);
if (r < 0)
{
_dbg_printf("fail to snprintf()\n");
return;
}
}
else
{
/*
<time> <program><<pid>, <severity> : <message>
*/
r = snprintf(msg, _MSG_MAX_LEN, "%s %s<%u>, %s: %s\n", date,
program_invocation_short_name, _pid_get(),
_level_str(level), str);
if (r < 0)
{
_dbg_printf("fail to snprintf()\n");
return;
}
}
#if _USE_SYSLOG
syslog(priority, "%s", msg);
#else // _USE_SYSLOG
printf("%s", msg);
#endif // _USE_SYSLOG
return;
} // zlog_log_f
/*!
* set log level to filter the logs
*
* @param [in] level log level
*
* @return true - successful
* false - failed
*/
bool zlog_levelSet(
const zlog_level_t level
)
{
if (level < 0 || level > ZLOG_LEVEL_DEBUG)
{
_dbg_printf("ERROR : invalid level %d\n", level);
return false;
}
_log_level = level;
return true;
}
/*!
* get current log level
*
* @return current log level
*/
zlog_level_t zlog_levelGet()
{
return _log_level;
}
/*!
* check if the log level is allowed
*
* @param [in] level log level
*
* @return true - the level is allowed
* false - the level is denied
*/
bool zlog_levelAllow(
const zlog_level_t level
)
{
if (level < 0 || level > _log_level)
{
return false;
}
return true;
}
+134
View File
@@ -0,0 +1,134 @@
#ifndef _ZLOG_API_H_
#define _ZLOG_API_H_
//==============================================================================
#include <stdbool.h>
//==============================================================================
typedef enum
{
ZLOG_FACI_USER, // user space application
ZLOG_FACI_KERNEL // kernel space daemon
} zlog_faci_t;
typedef enum
{
ZLOG_LEVEL_EMERG = 0,
ZLOG_LEVEL_ALERT,
ZLOG_LEVEL_CRITICAL,
ZLOG_LEVEL_ERROR,
ZLOG_LEVEL_WARNING,
ZLOG_LEVEL_NOTICE,
ZLOG_LEVEL_INFO,
ZLOG_LEVEL_DEBUG
} zlog_level_t;
//==============================================================================
/*!
* opens a connection to the system logger for a program
*
* @param [in] module_name module name. If NULL, the program name is used.
* @param [in] facility log facility
*
* @return true - successful
* false - failed
*/
bool zlog_open(
char *module,
zlog_faci_t facility
);
/*!
* generates a log message
*
* @param [in] level log level
* @param [in] file C file name
* @param [in] line line of the code
* @param [in] func API name
* @param [in] format string format of log message
* @param [in] ... any arguments required by the format
*
* @note no return value, reasons are
* 1. nothing can do it fail
* 2. if fail, the error message will pop up automatically
*/
void zlog_log_f(
const zlog_level_t level,
const char *file,
const int line,
const char *func,
const char *format,
...
);
/*!
* generates a log message
*
* @param [in] level log level
* @param [in] format string format of log message
* @param [in] ... any arguments required by the format
*
* @return true - successful
* false - failed
*/
bool zlog_log(
zlog_level_t level,
const char *format,
...
);
/*!
* set log level to filter the logs
*
* @param [in] level log level
*
* @return true - successful
* false - failed
*/
bool zlog_levelSet(
const zlog_level_t level
);
/*!
* get current log level
*
* @return current log level
*/
zlog_level_t zlog_levelGet();
/*!
* check if the log level is allowed
*
* @param [in] level log level
*
* @return true - the level is allowed
* false - the level is denied
*/
bool zlog_levelAllow(
const zlog_level_t level
);
//==============================================================================
/*
The macro is for internal use
*/
#define _ZLOG_MSG(_level_, _fmt_, ...) \
{\
if (zlog_levelAllow(_level_)) {\
zlog_log_f(_level_, __FILE__, __LINE__, __FUNCTION__, _fmt_, ##__VA_ARGS__);\
}\
}
//==============================================================================
/*
Public macro
*/
#define ZLOG_EMERG(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_EMERG, _fmt_, ##__VA_ARGS__)
#define ZLOG_ALERT(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_ALERT, _fmt_, ##__VA_ARGS__)
#define ZLOG_CRITICAL(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_CRITICAL, _fmt_, ##__VA_ARGS__)
#define ZLOG_ERROR(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_ERROR, _fmt_, ##__VA_ARGS__)
#define ZLOG_WARNING(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_WARNING, _fmt_, ##__VA_ARGS__)
#define ZLOG_NOTICE(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_NOTICE, _fmt_, ##__VA_ARGS__)
#define ZLOG_INFO(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_INFO, _fmt_, ##__VA_ARGS__)
#define ZLOG_DEBUG(_fmt_, ...) _ZLOG_MSG(ZLOG_LEVEL_DEBUG, _fmt_, ##__VA_ARGS__)
//==============================================================================
#endif // _ZLOG_API_H_