From 8f3474d878eb348c72c6366ae49265065055f0ae Mon Sep 17 00:00:00 2001 From: Jonathan Naylor Date: Mon, 5 Oct 2020 13:16:21 +0100 Subject: [PATCH] Updates for NetBSD. --- P25Gateway/Log.cpp | 40 ++++++++++++++++++++++++----------- P25Gateway/Log.h | 2 +- P25Gateway/P25Gateway.cpp | 6 +++++- P25Gateway/Thread.cpp | 34 +++++++++++++++++------------ P25Reflector/Log.cpp | 40 ++++++++++++++++++++++++----------- P25Reflector/Log.h | 2 +- P25Reflector/P25Reflector.cpp | 8 +++++-- P25Reflector/Thread.cpp | 34 +++++++++++++++++------------ 8 files changed, 109 insertions(+), 57 deletions(-) diff --git a/P25Gateway/Log.cpp b/P25Gateway/Log.cpp index fc37ebf..653c3a3 100644 --- a/P25Gateway/Log.cpp +++ b/P25Gateway/Log.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,6 +22,7 @@ #include #else #include +#include #endif #include @@ -36,6 +37,7 @@ static std::string m_filePath; static std::string m_fileRoot; static FILE* m_fpLog = NULL; +static bool m_daemon = false; static unsigned int m_displayLevel = 2U; @@ -45,6 +47,8 @@ static char LEVELS[] = " DMIWEF"; static bool LogOpen() { + bool status = false; + if (m_fileLevel == 0U) return true; @@ -68,30 +72,41 @@ static bool LogOpen() ::sprintf(filename, "%s/%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); #endif - m_fpLog = ::fopen(filename, "a+t"); + if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) + { + status = true; + +#if !defined(_WIN32) && !defined(_WIN64) + if (m_daemon) + dup2(fileno(m_fpLog), fileno(stderr)); +#endif + } + m_tm = *tm; - return m_fpLog != NULL; + return status; } -bool LogInitialise(const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) +bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) { m_filePath = filePath; m_fileRoot = fileRoot; m_fileLevel = fileLevel; m_displayLevel = displayLevel; - return ::LogOpen(); + m_daemon = daemon; + + return ::LogOpen(); } void LogFinalise() { - if (m_fpLog != NULL) - ::fclose(m_fpLog); + if (m_fpLog != NULL) + ::fclose(m_fpLog); } void Log(unsigned int level, const char* fmt, ...) { - assert(fmt != NULL); + assert(fmt != NULL); char buffer[300U]; #if defined(_WIN32) || defined(_WIN64) @@ -105,7 +120,7 @@ void Log(unsigned int level, const char* fmt, ...) struct tm* tm = ::gmtime(&now.tv_sec); - ::sprintf(buffer, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03lu ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000U); + ::sprintf(buffer, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03ld ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000L); #endif va_list vl; @@ -130,7 +145,8 @@ void Log(unsigned int level, const char* fmt, ...) } if (level == 6U) { // Fatal - ::fclose(m_fpLog); - exit(1); - } + ::fclose(m_fpLog); + exit(1); + } } + diff --git a/P25Gateway/Log.h b/P25Gateway/Log.h index d671ef9..0d00653 100644 --- a/P25Gateway/Log.h +++ b/P25Gateway/Log.h @@ -30,7 +30,7 @@ extern void Log(unsigned int level, const char* fmt, ...); -extern bool LogInitialise(const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); +extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); extern void LogFinalise(); #endif diff --git a/P25Gateway/P25Gateway.cpp b/P25Gateway/P25Gateway.cpp index 9d220b1..d2973c3 100644 --- a/P25Gateway/P25Gateway.cpp +++ b/P25Gateway/P25Gateway.cpp @@ -153,7 +153,11 @@ void CP25Gateway::run() } #endif - ret = ::LogInitialise(m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); +#if !defined(_WIN32) && !defined(_WIN64) + ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); +#else + ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); +#endif if (!ret) { ::fprintf(stderr, "P25Gateway: unable to open the log file\n"); return; diff --git a/P25Gateway/Thread.cpp b/P25Gateway/Thread.cpp index b334436..6f76f5b 100644 --- a/P25Gateway/Thread.cpp +++ b/P25Gateway/Thread.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,27 +31,27 @@ CThread::~CThread() bool CThread::run() { - m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL); + m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL); - return m_handle != NULL; + return m_handle != NULL; } void CThread::wait() { - ::WaitForSingleObject(m_handle, INFINITE); + ::WaitForSingleObject(m_handle, INFINITE); - ::CloseHandle(m_handle); + ::CloseHandle(m_handle); } DWORD CThread::helper(LPVOID arg) { - CThread* p = (CThread*)arg; + CThread* p = (CThread*)arg; - p->entry(); + p->entry(); - return 0UL; + return 0UL; } void CThread::sleep(unsigned int ms) @@ -74,28 +74,34 @@ CThread::~CThread() bool CThread::run() { - return ::pthread_create(&m_thread, NULL, helper, this) == 0; + return ::pthread_create(&m_thread, NULL, helper, this) == 0; } void CThread::wait() { - ::pthread_join(m_thread, NULL); + ::pthread_join(m_thread, NULL); } void* CThread::helper(void* arg) { - CThread* p = (CThread*)arg; + CThread* p = (CThread*)arg; - p->entry(); + p->entry(); - return NULL; + return NULL; } void CThread::sleep(unsigned int ms) { - ::usleep(ms * 1000); + struct timespec ts; + + ts.tv_sec = ms / 1000U; + ts.tv_nsec = (ms % 1000U) * 1000000U; + + ::nanosleep(&ts, NULL); } #endif + diff --git a/P25Reflector/Log.cpp b/P25Reflector/Log.cpp index fc37ebf..653c3a3 100644 --- a/P25Reflector/Log.cpp +++ b/P25Reflector/Log.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -22,6 +22,7 @@ #include #else #include +#include #endif #include @@ -36,6 +37,7 @@ static std::string m_filePath; static std::string m_fileRoot; static FILE* m_fpLog = NULL; +static bool m_daemon = false; static unsigned int m_displayLevel = 2U; @@ -45,6 +47,8 @@ static char LEVELS[] = " DMIWEF"; static bool LogOpen() { + bool status = false; + if (m_fileLevel == 0U) return true; @@ -68,30 +72,41 @@ static bool LogOpen() ::sprintf(filename, "%s/%s-%04d-%02d-%02d.log", m_filePath.c_str(), m_fileRoot.c_str(), tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); #endif - m_fpLog = ::fopen(filename, "a+t"); + if ((m_fpLog = ::fopen(filename, "a+t")) != NULL) + { + status = true; + +#if !defined(_WIN32) && !defined(_WIN64) + if (m_daemon) + dup2(fileno(m_fpLog), fileno(stderr)); +#endif + } + m_tm = *tm; - return m_fpLog != NULL; + return status; } -bool LogInitialise(const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) +bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel) { m_filePath = filePath; m_fileRoot = fileRoot; m_fileLevel = fileLevel; m_displayLevel = displayLevel; - return ::LogOpen(); + m_daemon = daemon; + + return ::LogOpen(); } void LogFinalise() { - if (m_fpLog != NULL) - ::fclose(m_fpLog); + if (m_fpLog != NULL) + ::fclose(m_fpLog); } void Log(unsigned int level, const char* fmt, ...) { - assert(fmt != NULL); + assert(fmt != NULL); char buffer[300U]; #if defined(_WIN32) || defined(_WIN64) @@ -105,7 +120,7 @@ void Log(unsigned int level, const char* fmt, ...) struct tm* tm = ::gmtime(&now.tv_sec); - ::sprintf(buffer, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03lu ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000U); + ::sprintf(buffer, "%c: %04d-%02d-%02d %02d:%02d:%02d.%03ld ", LEVELS[level], tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec, now.tv_usec / 1000L); #endif va_list vl; @@ -130,7 +145,8 @@ void Log(unsigned int level, const char* fmt, ...) } if (level == 6U) { // Fatal - ::fclose(m_fpLog); - exit(1); - } + ::fclose(m_fpLog); + exit(1); + } } + diff --git a/P25Reflector/Log.h b/P25Reflector/Log.h index d671ef9..0d00653 100644 --- a/P25Reflector/Log.h +++ b/P25Reflector/Log.h @@ -30,7 +30,7 @@ extern void Log(unsigned int level, const char* fmt, ...); -extern bool LogInitialise(const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); +extern bool LogInitialise(bool daemon, const std::string& filePath, const std::string& fileRoot, unsigned int fileLevel, unsigned int displayLevel); extern void LogFinalise(); #endif diff --git a/P25Reflector/P25Reflector.cpp b/P25Reflector/P25Reflector.cpp index 721b25c..8728540 100644 --- a/P25Reflector/P25Reflector.cpp +++ b/P25Reflector/P25Reflector.cpp @@ -1,5 +1,5 @@ /* -* Copyright (C) 2016,2018 by Jonathan Naylor G4KLX +* Copyright (C) 2016,2018,2020 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -150,7 +150,11 @@ void CP25Reflector::run() } #endif - ret = ::LogInitialise(m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); +#if !defined(_WIN32) && !defined(_WIN64) + ret = ::LogInitialise(m_daemon, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); +#else + ret = ::LogInitialise(false, m_conf.getLogFilePath(), m_conf.getLogFileRoot(), m_conf.getLogFileLevel(), m_conf.getLogDisplayLevel()); +#endif if (!ret) { ::fprintf(stderr, "P25Gateway: unable to open the log file\n"); return; diff --git a/P25Reflector/Thread.cpp b/P25Reflector/Thread.cpp index b334436..6f76f5b 100644 --- a/P25Reflector/Thread.cpp +++ b/P25Reflector/Thread.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015,2016 by Jonathan Naylor G4KLX + * Copyright (C) 2015,2016,2020 by Jonathan Naylor G4KLX * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -31,27 +31,27 @@ CThread::~CThread() bool CThread::run() { - m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL); + m_handle = ::CreateThread(NULL, 0, &helper, this, 0, NULL); - return m_handle != NULL; + return m_handle != NULL; } void CThread::wait() { - ::WaitForSingleObject(m_handle, INFINITE); + ::WaitForSingleObject(m_handle, INFINITE); - ::CloseHandle(m_handle); + ::CloseHandle(m_handle); } DWORD CThread::helper(LPVOID arg) { - CThread* p = (CThread*)arg; + CThread* p = (CThread*)arg; - p->entry(); + p->entry(); - return 0UL; + return 0UL; } void CThread::sleep(unsigned int ms) @@ -74,28 +74,34 @@ CThread::~CThread() bool CThread::run() { - return ::pthread_create(&m_thread, NULL, helper, this) == 0; + return ::pthread_create(&m_thread, NULL, helper, this) == 0; } void CThread::wait() { - ::pthread_join(m_thread, NULL); + ::pthread_join(m_thread, NULL); } void* CThread::helper(void* arg) { - CThread* p = (CThread*)arg; + CThread* p = (CThread*)arg; - p->entry(); + p->entry(); - return NULL; + return NULL; } void CThread::sleep(unsigned int ms) { - ::usleep(ms * 1000); + struct timespec ts; + + ts.tv_sec = ms / 1000U; + ts.tv_nsec = (ms % 1000U) * 1000000U; + + ::nanosleep(&ts, NULL); } #endif +