Skip to content

Commit

Permalink
Migrate to syslog.
Browse files Browse the repository at this point in the history
  • Loading branch information
rozhuk-im committed Mar 15, 2024
1 parent 83bc647 commit cfef57f
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 143 deletions.
6 changes: 3 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
#

############################# INITIAL SECTION ##########################
cmake_minimum_required(VERSION 3.10)
cmake_minimum_required(VERSION 3.20)

project(msd_lite C)

set(PACKAGE_VERSION_MAJOR 1)
set(PACKAGE_VERSION_MINOR 10)
set(PACKAGE_VERSION_PATCH 1)
set(PACKAGE_VERSION_MINOR 11)
set(PACKAGE_VERSION_PATCH 0)

set(PACKAGE_NAME "${PROJECT_NAME}")
set(PACKAGE_VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH}")
Expand Down
4 changes: 1 addition & 3 deletions conf/msd_lite.conf
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ available.

<msd>
<log>
<file>/root/msd_lite/msd_lite.log</file>
<level>6</level> <!-- syslog Severity level: 0=emerg - 7=debug. -->
</log>

<threadPool>
<threadsCountMax>1</threadsCountMax> <!-- 0 = auto -->
<fBindToCPU>yes</fBindToCPU> <!-- Bind threads to CPUs. -->
<fCacheGetTimeSyscall>yes</fCacheGetTimeSyscall> <!-- Cache gettime() syscalls.. -->
<timerGranularity>100</timerGranularity> <!-- 1/1000 sec -->
</threadPool>


Expand Down
8 changes: 3 additions & 5 deletions msd_lite.project
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@
</VirtualDirectory>
<VirtualDirectory Name="utils">
<File Name="src/liblcb/src/utils/info.c"/>
<File Name="src/liblcb/src/utils/log.c"/>
<File Name="src/liblcb/src/utils/ring_buffer.c"/>
<File Name="src/liblcb/src/utils/xml.c"/>
<File Name="src/liblcb/src/utils/cmd_line_daemon.c"/>
Expand Down Expand Up @@ -89,7 +88,6 @@
<File Name="src/liblcb/include/utils/hash_bucket.h"/>
<File Name="src/liblcb/include/utils/info.h"/>
<File Name="src/liblcb/include/utils/io_buf.h"/>
<File Name="src/liblcb/include/utils/log.h"/>
<File Name="src/liblcb/include/utils/macro.h"/>
<File Name="src/liblcb/include/utils/mem_utils.h"/>
<File Name="src/liblcb/include/utils/ring_buffer.h"/>
Expand Down Expand Up @@ -126,7 +124,7 @@
<Compiler Options="-g -DDEBUG" C_Options="-ftrapv;-g -DDEBUG;-O0;-fwrapv;-fstack-protector-all;-Wall;-g3 -ggdb;-Wno-reserved-id-macro;-Wno-gnu-zero-variadic-macro-arguments;-Wno-variadic-macros;-Wno-documentation;-Wno-documentation-unknown-command;-Wno-padded;-Wno-cast-qual;-Wno-cast-function-type-strict;-Wno-reserved-identifier;-Wno-unsafe-buffer-usage" Assembler="" Required="yes" PreCompiledHeader="" PCHInCommandLine="no" PCHFlags="" PCHFlagsPolicy="0"/>
<Linker Options="" Required="yes"/>
<ResourceCompiler Options="" Required="no"/>
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="$(ProjectPath)/build/src/" Command="$(OutputFile)" CommandArguments="-v -c /home/rim/docs/Progs/msd/msd_lite/msd_lite.conf" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
<General OutputFile="$(IntermediateDirectory)/$(ProjectName)" IntermediateDirectory="$(ProjectPath)/build/src/" Command="$(OutputFile)" CommandArguments="-v -c /home/rim/docs/Progs/msd/msd_lite/conf/msd_lite.conf" UseSeparateDebugArgs="no" DebugArguments="" WorkingDirectory="$(IntermediateDirectory)" PauseExecWhenProcTerminates="yes" IsGUIProgram="no" IsEnabled="yes"/>
<BuildSystem Name="Default"/>
<Environment EnvVarSetName="&lt;Use Defaults&gt;" DbgSetName="&lt;Use Defaults&gt;">
<![CDATA[]]>
Expand Down Expand Up @@ -190,12 +188,12 @@ make</BuildCommand>
<CustomBuild Enabled="yes">
<Target Name="Configure">mkdir $(ProjectPath)/$(ConfigurationName) &amp;&amp;
cd $(ProjectPath)/$(ConfigurationName) &amp;&amp;
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=true -DENABLE_ALSA=true ..
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=true ..
</Target>
<RebuildCommand>rm -rf $(ProjectPath)/$(ConfigurationName) &amp;&amp;
mkdir $(ProjectPath)/$(ConfigurationName) &amp;&amp;
cd $(ProjectPath)/$(ConfigurationName) &amp;&amp;
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=true -DENABLE_ALSA=true .. &amp;&amp;
cmake -DCMAKE_BUILD_TYPE=Debug -DCMAKE_VERBOSE_MAKEFILE=true .. &amp;&amp;
make -C $(ProjectPath)/$(ConfigurationName) -j`getconf NPROCESSORS_ONLN`
</RebuildCommand>
<CleanCommand>rm -rf $(ProjectPath)/$(ConfigurationName)</CleanCommand>
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ set(MSD_LITE_BIN msd_lite.c
liblcb/src/utils/buf_str.c
liblcb/src/utils/info.c
liblcb/src/utils/ring_buffer.c
liblcb/src/utils/log.c
liblcb/src/utils/xml.c)

add_executable(msd_lite ${MSD_LITE_BIN})
Expand Down
86 changes: 37 additions & 49 deletions src/msd_lite.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2012-2023 Rozhuk Ivan <[email protected]>
* Copyright (c) 2012-2024 Rozhuk Ivan <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -50,6 +50,7 @@
#include <unistd.h> /* close, write, sysconf */
#include <fcntl.h> /* open, fcntl */
#include <signal.h> /* SIGNAL constants. */
#include <syslog.h>


#include "utils/mem_utils.h"
Expand All @@ -65,7 +66,6 @@
#include "threadpool/threadpool_task.h"
#include "utils/buf_str.h"
#include "utils/sys.h"
#include "utils/log.h"
#include "proto/http_server.h"
#include "utils/info.h"
#include "utils/cmd_line_daemon.h"
Expand Down Expand Up @@ -277,7 +277,6 @@ msd_src_conn_profile_load(const uint8_t *data, size_t data_size, void *conn) {
int
main(int argc, char *argv[]) {
int error = 0;
int log_fd = -1;
uint8_t *cfg_file_buf = NULL;
size_t tm, cfg_file_buf_size = 0;
tp_p tp;
Expand All @@ -293,60 +292,49 @@ main(int argc, char *argv[]) {
}
if (0 != cmd_line_data.daemon) {
make_daemon();
openlog(PACKAGE_NAME,
(LOG_NDELAY | LOG_PID | ((0 != cmd_line_data.verbose) ? LOG_PERROR : 0)),
LOG_DAEMON);
} else {
openlog(PACKAGE_NAME,
(LOG_NDELAY | LOG_PID | LOG_PERROR), LOG_USER);
}
setlogmask(LOG_UPTO(cmd_line_data.log_level));

{ // process config file
{ /* Process config file. */
const uint8_t *data;
char strbuf[1024];
size_t data_size;
tp_settings_t tp_s;
http_srv_cli_ccb_t ccb;
http_srv_settings_t http_s;

g_log_fd = (uintptr_t)open("/dev/stderr", (O_WRONLY | O_APPEND));
error = read_file(cmd_line_data.cfg_file_name, 0, 0, 0,
CFG_FILE_MAX_SIZE, &cfg_file_buf, &cfg_file_buf_size);
if (0 != error) {
LOG_ERR(error, "config read_file()");
SYSLOG_ERR(LOG_CRIT, error, "config read_file().");
goto err_out;
}
if (0 != xml_get_val_args(cfg_file_buf, cfg_file_buf_size,
NULL, NULL, NULL, NULL, NULL,
(const uint8_t*)"msd", NULL)) {
LOG_INFO("Config file XML format invalid.");
syslog(LOG_CRIT, "Config file XML format invalid.");
goto err_out;
}

/* Log file */
if (0 == cmd_line_data.verbose &&
0 == MSD_CFG_GET_VAL_DATA(NULL, &data, &data_size,
"log", "file", NULL)) {
if (sizeof(strbuf) > data_size) {
memcpy(strbuf, data, data_size);
strbuf[data_size] = 0;
log_fd = open(strbuf,
(O_WRONLY | O_APPEND | O_CREAT), 0644);
if (-1 == log_fd) {
LOG_ERR(errno, "Fail to open log file.");
}
} else {
LOG_ERR(EINVAL, "Log file name too long.");
}
} else if (0 != cmd_line_data.verbose) {
log_fd = open("/dev/stdout", (O_WRONLY | O_APPEND));
}
close((int)g_log_fd);
g_log_fd = (uintptr_t)log_fd;
fd_set_nonblocking(g_log_fd, 1);
log_write("\n\n\n\n", 4);
LOG_INFO(PACKAGE_STRING": started");
/* Log level. */
if (0 == MSD_CFG_GET_VAL_UINT(NULL, (uint32_t*)&cmd_line_data.log_level,
"log", "level", NULL)) {
setlogmask(LOG_UPTO(cmd_line_data.log_level));
}
syslog(LOG_NOTICE, PACKAGE_STRING": started!");
#ifdef DEBUG
LOG_INFO("Build: "__DATE__" "__TIME__", DEBUG");
syslog(LOG_INFO, "Build: "__DATE__" "__TIME__", DEBUG.");
#else
LOG_INFO("Build: "__DATE__" "__TIME__", Release");
syslog(LOG_INFO, "Build: "__DATE__" "__TIME__", Release.");
#endif
LOG_INFO_FMT("CPU count: %d", get_cpu_count());
LOG_INFO_FMT("descriptor table size: %d (max files)", getdtablesize());
syslog(LOG_INFO, "CPU count: %d.", get_cpu_count());
syslog(LOG_INFO, "Descriptor table size: %d (max files).", getdtablesize());


/* System resource limits. */
if (0 == MSD_CFG_GET_VAL_DATA(NULL, &data, &data_size,
Expand All @@ -362,16 +350,16 @@ main(int argc, char *argv[]) {
}
error = tp_create(&tp_s, &tp);
if (0 != error) {
LOG_ERR(error, "tp_create()");
SYSLOG_ERR(LOG_CRIT, error, "tp_create().");
goto err_out;
}
tp_threads_create(tp, 1);// XXX exit rewrite
tp_threads_create(tp, 1); /* XXX exit rewrite. */


/* HTTP server settings. */
/* Read from config. */
if (0 != MSD_CFG_GET_VAL_DATA(NULL, &data, &data_size, "HTTP", NULL)) {
LOG_INFO("No HTTP server settings, nothink to do...");
syslog(LOG_NOTICE, "No HTTP server settings, nothink to do...");
goto err_out;
}
http_srv_def_settings(1, PACKAGE_NAME"/"PACKAGE_VERSION, 1, &http_s);
Expand All @@ -385,7 +373,7 @@ main(int argc, char *argv[]) {
NULL, &ccb, &http_s, &g_data,
&g_data.http_srv);
if (0 != error) {
LOG_ERR(error, "http_srv_xml_load_start()");
SYSLOG_ERR(LOG_CRIT, error, "http_srv_xml_load_start().");
goto err_out;
}

Expand All @@ -411,9 +399,10 @@ main(int argc, char *argv[]) {
error = str_hubs_bckt_create(tp, PACKAGE_NAME"/"PACKAGE_VERSION, &g_data.hub_params,
&g_data.src_params, &g_data.shbskt);
if (0 != error) {
LOG_ERR(error, "str_hubs_bckt_create()");
SYSLOG_ERR(LOG_CRIT, error, "str_hubs_bckt_create().");
goto err_out;
}
free(cfg_file_buf);
} /* Done with config. */


Expand Down Expand Up @@ -446,9 +435,8 @@ main(int argc, char *argv[]) {
}

tp_destroy(tp);
LOG_INFO("exiting.");
close((int)g_data.log_fd);
free(cfg_file_buf);
syslog(LOG_NOTICE, "Exiting.");
closelog();

err_out:
return (error);
Expand All @@ -469,7 +457,7 @@ msd_http_srv_hub_attach(http_srv_cli_p cli, uint8_t *hub_name, size_t hub_name_s
tp_task_p tptask;
uintptr_t skt;

LOGD_EV("...");
SYSLOGD_EX(LOG_DEBUG, "...");

if (NULL == cli || NULL == hub_name)
return (EINVAL);
Expand All @@ -491,7 +479,7 @@ msd_http_srv_hub_attach(http_srv_cli_p cli, uint8_t *hub_name, size_t hub_name_s
}
strh_cli = str_hub_cli_alloc(skt, (const char*)ptm, tm);
if (NULL == strh_cli) {
LOG_ERR(ENOMEM, "str_hub_cli_alloc()");
syslog(LOG_ERR, "str_hub_cli_alloc().");
return (ENOMEM);
}
/*
Expand All @@ -508,14 +496,14 @@ msd_http_srv_hub_attach(http_srv_cli_p cli, uint8_t *hub_name, size_t hub_name_s
sa_copy(&strh_cli->remonte_addr, &strh_cli->xreal_addr);
}

LOGD_INFO_FMT("%s - : attach...", hub_name);
SYSLOGD_EX(LOG_DEBUG, "%s - : attach...", hub_name);
error = str_hub_cli_attach(g_data.shbskt, strh_cli, hub_name, hub_name_size,
src_conn_params);
/* Do not read/write to stream hub client, stream hub is new owner! */
if (0 != error) {
strh_cli->skt = (uintptr_t)-1;
str_hub_cli_destroy(NULL, strh_cli);
LOG_ERR(error, "str_hub_cli_attach()");
SYSLOG_ERR(LOG_ERR, error, "str_hub_cli_attach().");
} else {
tp_task_flags_del(tptask, TP_TASK_F_CLOSE_ON_DESTROY);
http_srv_cli_free(cli);
Expand All @@ -533,7 +521,7 @@ msd_http_req_url_parse(http_srv_req_p req, struct sockaddr_storage *ssaddr,
char straddr[STR_ADDR_LEN], ifname[(IFNAMSIZ + 1)];
struct sockaddr_storage ss;

LOGD_EV("...");
SYSLOGD_EX(LOG_DEBUG, "...");

if (NULL == req || NULL == hub_name || 0 == hub_name_size)
return (500);
Expand Down Expand Up @@ -613,7 +601,7 @@ msd_http_srv_on_req_rcv_cb(http_srv_cli_p cli, void *udata __unused,
static const char *cttype = "Content-Type: text/plain\r\n"
"Pragma: no-cache";

LOGD_EV("...");
SYSLOGD_EX(LOG_DEBUG, "...");

if (HTTP_REQ_METHOD_GET != req->line.method_code &&
HTTP_REQ_METHOD_HEAD != req->line.method_code) {
Expand Down
3 changes: 1 addition & 2 deletions src/msd_lite_stat_text.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*-
* Copyright (c) 2012 - 2021 Rozhuk Ivan <[email protected]>
* Copyright (c) 2012-2024 Rozhuk Ivan <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
Expand Down Expand Up @@ -57,7 +57,6 @@
#include "proto/http_server.h"
#include "stream_sys.h"
#include "utils/info.h"
#include "utils/log.h"
#include "msd_lite_stat_text.h"


Expand Down
Loading

0 comments on commit cfef57f

Please sign in to comment.