parent
e6777232f9
commit
305f49c13c
@ -1,136 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2015,2016 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
|
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Log.h"
|
|
||||||
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
|
||||||
#include <Windows.h>
|
|
||||||
#else
|
|
||||||
#include <sys/time.h>
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cstdlib>
|
|
||||||
#include <cstdarg>
|
|
||||||
#include <ctime>
|
|
||||||
#include <cassert>
|
|
||||||
#include <cstring>
|
|
||||||
|
|
||||||
static unsigned int m_fileLevel = 2U;
|
|
||||||
static std::string m_filePath;
|
|
||||||
static std::string m_fileRoot;
|
|
||||||
|
|
||||||
static FILE* m_fpLog = NULL;
|
|
||||||
|
|
||||||
static unsigned int m_displayLevel = 2U;
|
|
||||||
|
|
||||||
static struct tm m_tm;
|
|
||||||
|
|
||||||
static char LEVELS[] = " DMIWEF";
|
|
||||||
|
|
||||||
static bool LogOpen()
|
|
||||||
{
|
|
||||||
if (m_fileLevel == 0U)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
time_t now;
|
|
||||||
::time(&now);
|
|
||||||
|
|
||||||
struct tm* tm = ::gmtime(&now);
|
|
||||||
|
|
||||||
if (tm->tm_mday == m_tm.tm_mday && tm->tm_mon == m_tm.tm_mon && tm->tm_year == m_tm.tm_year) {
|
|
||||||
if (m_fpLog != NULL)
|
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
if (m_fpLog != NULL)
|
|
||||||
::fclose(m_fpLog);
|
|
||||||
}
|
|
||||||
|
|
||||||
char filename[100U];
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
|
||||||
::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);
|
|
||||||
#else
|
|
||||||
::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");
|
|
||||||
m_tm = *tm;
|
|
||||||
|
|
||||||
return m_fpLog != NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool LogInitialise(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();
|
|
||||||
}
|
|
||||||
|
|
||||||
void LogFinalise()
|
|
||||||
{
|
|
||||||
if (m_fpLog != NULL)
|
|
||||||
::fclose(m_fpLog);
|
|
||||||
}
|
|
||||||
|
|
||||||
void Log(unsigned int level, const char* fmt, ...)
|
|
||||||
{
|
|
||||||
assert(fmt != NULL);
|
|
||||||
|
|
||||||
char buffer[300U];
|
|
||||||
#if defined(_WIN32) || defined(_WIN64)
|
|
||||||
SYSTEMTIME st;
|
|
||||||
::GetSystemTime(&st);
|
|
||||||
|
|
||||||
::sprintf(buffer, "%c: %04u-%02u-%02u %02u:%02u:%02u.%03u ", LEVELS[level], st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds);
|
|
||||||
#else
|
|
||||||
struct timeval now;
|
|
||||||
::gettimeofday(&now, NULL);
|
|
||||||
|
|
||||||
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);
|
|
||||||
#endif
|
|
||||||
|
|
||||||
va_list vl;
|
|
||||||
va_start(vl, fmt);
|
|
||||||
|
|
||||||
::vsprintf(buffer + ::strlen(buffer), fmt, vl);
|
|
||||||
|
|
||||||
va_end(vl);
|
|
||||||
|
|
||||||
if (level >= m_fileLevel && m_fileLevel != 0U) {
|
|
||||||
bool ret = ::LogOpen();
|
|
||||||
if (!ret)
|
|
||||||
return;
|
|
||||||
|
|
||||||
::fprintf(m_fpLog, "%s\n", buffer);
|
|
||||||
::fflush(m_fpLog);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level >= m_displayLevel && m_displayLevel != 0U) {
|
|
||||||
::fprintf(stdout, "%s\n", buffer);
|
|
||||||
::fflush(stdout);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (level == 6U) { // Fatal
|
|
||||||
::fclose(m_fpLog);
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2015,2016 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
|
|
||||||
* the Free Software Foundation; either version 2 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with this program; if not, write to the Free Software
|
|
||||||
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#if !defined(LOG_H)
|
|
||||||
#define LOG_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
#define LogDebug(fmt, ...) Log(1U, fmt, ##__VA_ARGS__)
|
|
||||||
#define LogMessage(fmt, ...) Log(2U, fmt, ##__VA_ARGS__)
|
|
||||||
#define LogInfo(fmt, ...) Log(3U, fmt, ##__VA_ARGS__)
|
|
||||||
#define LogWarning(fmt, ...) Log(4U, fmt, ##__VA_ARGS__)
|
|
||||||
#define LogError(fmt, ...) Log(5U, fmt, ##__VA_ARGS__)
|
|
||||||
#define LogFatal(fmt, ...) Log(6U, fmt, ##__VA_ARGS__)
|
|
||||||
|
|
||||||
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 void LogFinalise();
|
|
||||||
|
|
||||||
#endif
|
|
@ -1,146 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2009,2014,2015,2016 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
|
|
||||||
* the Free Software Foundation; version 2 of the License.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#include "Utils.h"
|
|
||||||
#include "Log.h"
|
|
||||||
|
|
||||||
#include <cstdio>
|
|
||||||
#include <cassert>
|
|
||||||
|
|
||||||
void CUtils::dump(const std::string& title, const unsigned char* data, unsigned int length)
|
|
||||||
{
|
|
||||||
assert(data != NULL);
|
|
||||||
|
|
||||||
dump(2U, title, data, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUtils::dump(int level, const std::string& title, const unsigned char* data, unsigned int length)
|
|
||||||
{
|
|
||||||
assert(data != NULL);
|
|
||||||
|
|
||||||
::Log(level, "%s", title.c_str());
|
|
||||||
|
|
||||||
unsigned int offset = 0U;
|
|
||||||
|
|
||||||
while (length > 0U) {
|
|
||||||
std::string output;
|
|
||||||
|
|
||||||
unsigned int bytes = (length > 16U) ? 16U : length;
|
|
||||||
|
|
||||||
for (unsigned i = 0U; i < bytes; i++) {
|
|
||||||
char temp[10U];
|
|
||||||
::sprintf(temp, "%02X ", data[offset + i]);
|
|
||||||
output += temp;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (unsigned int i = bytes; i < 16U; i++)
|
|
||||||
output += " ";
|
|
||||||
|
|
||||||
output += " *";
|
|
||||||
|
|
||||||
for (unsigned i = 0U; i < bytes; i++) {
|
|
||||||
unsigned char c = data[offset + i];
|
|
||||||
|
|
||||||
if (::isprint(c))
|
|
||||||
output += c;
|
|
||||||
else
|
|
||||||
output += '.';
|
|
||||||
}
|
|
||||||
|
|
||||||
output += '*';
|
|
||||||
|
|
||||||
::Log(level, "%04X: %s", offset, output.c_str());
|
|
||||||
|
|
||||||
offset += 16U;
|
|
||||||
|
|
||||||
if (length >= 16U)
|
|
||||||
length -= 16U;
|
|
||||||
else
|
|
||||||
length = 0U;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUtils::dump(const std::string& title, const bool* bits, unsigned int length)
|
|
||||||
{
|
|
||||||
assert(bits != NULL);
|
|
||||||
|
|
||||||
dump(2U, title, bits, length);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUtils::dump(int level, const std::string& title, const bool* bits, unsigned int length)
|
|
||||||
{
|
|
||||||
assert(bits != NULL);
|
|
||||||
|
|
||||||
unsigned char bytes[100U];
|
|
||||||
unsigned int nBytes = 0U;
|
|
||||||
for (unsigned int n = 0U; n < length; n += 8U, nBytes++)
|
|
||||||
bitsToByteBE(bits + n, bytes[nBytes]);
|
|
||||||
|
|
||||||
dump(level, title, bytes, nBytes);
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUtils::byteToBitsBE(unsigned char byte, bool* bits)
|
|
||||||
{
|
|
||||||
assert(bits != NULL);
|
|
||||||
|
|
||||||
bits[0U] = (byte & 0x80U) == 0x80U;
|
|
||||||
bits[1U] = (byte & 0x40U) == 0x40U;
|
|
||||||
bits[2U] = (byte & 0x20U) == 0x20U;
|
|
||||||
bits[3U] = (byte & 0x10U) == 0x10U;
|
|
||||||
bits[4U] = (byte & 0x08U) == 0x08U;
|
|
||||||
bits[5U] = (byte & 0x04U) == 0x04U;
|
|
||||||
bits[6U] = (byte & 0x02U) == 0x02U;
|
|
||||||
bits[7U] = (byte & 0x01U) == 0x01U;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUtils::byteToBitsLE(unsigned char byte, bool* bits)
|
|
||||||
{
|
|
||||||
assert(bits != NULL);
|
|
||||||
|
|
||||||
bits[0U] = (byte & 0x01U) == 0x01U;
|
|
||||||
bits[1U] = (byte & 0x02U) == 0x02U;
|
|
||||||
bits[2U] = (byte & 0x04U) == 0x04U;
|
|
||||||
bits[3U] = (byte & 0x08U) == 0x08U;
|
|
||||||
bits[4U] = (byte & 0x10U) == 0x10U;
|
|
||||||
bits[5U] = (byte & 0x20U) == 0x20U;
|
|
||||||
bits[6U] = (byte & 0x40U) == 0x40U;
|
|
||||||
bits[7U] = (byte & 0x80U) == 0x80U;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUtils::bitsToByteBE(const bool* bits, unsigned char& byte)
|
|
||||||
{
|
|
||||||
assert(bits != NULL);
|
|
||||||
|
|
||||||
byte = bits[0U] ? 0x80U : 0x00U;
|
|
||||||
byte |= bits[1U] ? 0x40U : 0x00U;
|
|
||||||
byte |= bits[2U] ? 0x20U : 0x00U;
|
|
||||||
byte |= bits[3U] ? 0x10U : 0x00U;
|
|
||||||
byte |= bits[4U] ? 0x08U : 0x00U;
|
|
||||||
byte |= bits[5U] ? 0x04U : 0x00U;
|
|
||||||
byte |= bits[6U] ? 0x02U : 0x00U;
|
|
||||||
byte |= bits[7U] ? 0x01U : 0x00U;
|
|
||||||
}
|
|
||||||
|
|
||||||
void CUtils::bitsToByteLE(const bool* bits, unsigned char& byte)
|
|
||||||
{
|
|
||||||
assert(bits != NULL);
|
|
||||||
|
|
||||||
byte = bits[0U] ? 0x01U : 0x00U;
|
|
||||||
byte |= bits[1U] ? 0x02U : 0x00U;
|
|
||||||
byte |= bits[2U] ? 0x04U : 0x00U;
|
|
||||||
byte |= bits[3U] ? 0x08U : 0x00U;
|
|
||||||
byte |= bits[4U] ? 0x10U : 0x00U;
|
|
||||||
byte |= bits[5U] ? 0x20U : 0x00U;
|
|
||||||
byte |= bits[6U] ? 0x40U : 0x00U;
|
|
||||||
byte |= bits[7U] ? 0x80U : 0x00U;
|
|
||||||
}
|
|
@ -1,36 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2009,2014,2015 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
|
|
||||||
* the Free Software Foundation; version 2 of the License.
|
|
||||||
*
|
|
||||||
* This program is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*/
|
|
||||||
|
|
||||||
#ifndef Utils_H
|
|
||||||
#define Utils_H
|
|
||||||
|
|
||||||
#include <string>
|
|
||||||
|
|
||||||
class CUtils {
|
|
||||||
public:
|
|
||||||
static void dump(const std::string& title, const unsigned char* data, unsigned int length);
|
|
||||||
static void dump(int level, const std::string& title, const unsigned char* data, unsigned int length);
|
|
||||||
|
|
||||||
static void dump(const std::string& title, const bool* bits, unsigned int length);
|
|
||||||
static void dump(int level, const std::string& title, const bool* bits, unsigned int length);
|
|
||||||
|
|
||||||
static void byteToBitsBE(unsigned char byte, bool* bits);
|
|
||||||
static void byteToBitsLE(unsigned char byte, bool* bits);
|
|
||||||
|
|
||||||
static void bitsToByteBE(const bool* bits, unsigned char& byte);
|
|
||||||
static void bitsToByteLE(const bool* bits, unsigned char& byte);
|
|
||||||
|
|
||||||
private:
|
|
||||||
};
|
|
||||||
|
|
||||||
#endif
|
|
Loading…
Reference in new issue