402 lines
24 KiB
C++
402 lines
24 KiB
C++
#pragma once
|
|
|
|
extern __thread char tls_error[256];
|
|
|
|
#include "clxlogHeader.hpp"
|
|
#include "presetMsg.h"
|
|
#include "appliName.hpp"
|
|
#include "stringsList.hpp"
|
|
|
|
#include <allocator.hpp> // In base/utils.
|
|
|
|
#include <syslog.h>
|
|
#include <string>
|
|
#include <set>
|
|
|
|
namespace clxlog
|
|
{
|
|
# ifndef LOG_EMERGENCY
|
|
# define LOG_EMERGENCY LOG_EMERG
|
|
# endif
|
|
# ifndef LOG_CRITICAL
|
|
# define LOG_CRITICAL LOG_CRIT
|
|
# endif
|
|
# ifndef LOG_ERROR
|
|
# define LOG_ERROR LOG_ERR
|
|
# endif
|
|
# ifndef LOG_WARN
|
|
# define LOG_WARN LOG_WARNING
|
|
# endif
|
|
# define LOG_INTERNAL (-1)
|
|
|
|
enum FieldType
|
|
{
|
|
FieldConstString = ClxlogFieldConstString, // Represent a constant string get from the format.
|
|
|
|
// The following types must be followed by the size of the parameter.
|
|
ParamStruct = ClxlogParamStruct, // A structure; it must be followed by its size. Its used fields are defined by using Field... keywords.
|
|
ParamString = ClxlogParamString, // A pointer to a null terminated string.
|
|
ParamInteger = ClxlogParamInteger, // An integer.
|
|
ParamUinteger = ClxlogParamUinteger, // An unsigned integer.
|
|
ParamPointer = ClxlogParamPointer, // A pointer.
|
|
ParamGxaOrderId = ClxlogParamGxaOrderId, // If the first character is 0xFF, the parameter is a 64 bits unsigned integer. Otherwise, it is a string.
|
|
ParamStringFromList = ClxlogParamStringFromList, // A string from a strings list; must be followed by the list ID (returned by createStringsList) and the key of the string.
|
|
ParamLast = ClxlogParamLast, // Indicates that the last parameter was reached.
|
|
ParamScaleInteger = ClxlogParamScaleInteger, // A signed double stored in an integer.
|
|
ParamScaleUinteger = ClxlogParamScaleUinteger, // An unsigned double stored in an integer.
|
|
ParamChar = ClxlogParamChar, // An unique character.
|
|
|
|
// The following types describe the content of a PresetMsgField parameter.
|
|
FieldInteger = ClxlogFieldInteger, // An integer.
|
|
FieldUinteger = ClxlogFieldUinteger, // An unsigned integer.
|
|
FieldString = ClxlogFieldString, // A pointer to a null terminated string (0 is allowed).
|
|
FieldCharArray = ClxlogFieldCharArray, // An array of characters.
|
|
FieldCharArrayPadded = ClxlogFieldCharArrayPadded, // An array of characters right padded by space.
|
|
FieldPointer = ClxlogFieldPointer, // A pointer.
|
|
FieldGxaOrderId = ClxlogFieldGxaOrderId, // If the first character is 0xFF, the parameter is a 64 bits unsigned integer. Otherwise, it is a string.
|
|
FieldGxaOrderIdPtr = ClxlogFieldGxaOrderIdPtr, // If the first character is 0xFF, the parameter is a 64 bits unsigned integer. Otherwise, it is a string.
|
|
FieldStringFromList = ClxlogFieldStringFromList, // A string from a strings list; must be followed by the list ID (returned by createStringsList) and the key of the string.
|
|
FieldScaleInteger = ClxlogFieldScaleInteger,
|
|
FieldScaleUinteger = ClxlogFieldScaleUinteger,
|
|
FieldChar = ClxlogFieldChar,
|
|
|
|
FieldLast = ClxlogFieldLast // Indicates that the last field of the struct was reached.
|
|
};
|
|
|
|
extern char const* getLevelStr(int p_level);
|
|
extern int convertLevelStr(const char* p_levelstr);
|
|
|
|
class Writer;
|
|
class PrintableMsg;
|
|
|
|
/*--------------------------------------------------------------------------------------------------
|
|
// ____ _ __ __ ____
|
|
// | _ \ _ __ ___ ___ ___| |_| \/ |___ __ _| _ \ __ _ _ __ __ _ _ __ ___
|
|
// | |_) | '__/ _ \/ __|/ _ \ __| |\/| / __|/ _` | |_) / _` | '__/ _` | '_ ` _ \
|
|
// | __/| | | __/\__ \ __/ |_| | | \__ \ (_| | __/ (_| | | | (_| | | | | | |
|
|
// |_| |_| \___||___/\___|\__|_| |_|___/\__, |_| \__,_|_| \__,_|_| |_| |_|
|
|
// |___/
|
|
--------------------------------------------------------------------------------------------------*/
|
|
|
|
struct PresetMsgParam
|
|
{
|
|
// Methods.
|
|
// ---------------------------------------------------------------------------------------------
|
|
public: bool operator<(PresetMsgParam const& p_right) const
|
|
{
|
|
return(offset < p_right.offset);
|
|
}
|
|
|
|
// Members.
|
|
// ---------------------------------------------------------------------------------------------
|
|
public: FieldType type;
|
|
public: bool inStructure; // true if param is a part of a structure.
|
|
public: bool hasPresenceBit; // Only available when 'inStructure' is true. set to true if a bit indicates that the param is available or not.
|
|
public: size_t firstOffset; // Only available when 'inStructure' is true.
|
|
public: size_t offset; // If 'inStructure' == false, offset points in the data sent to clxlogd, otherwise it points in the structure given to the presetMsg method.
|
|
public: size_t size; // The size of the data.
|
|
public: size_t fieldIndex; // The field to witch the parameter is linked.
|
|
};
|
|
|
|
/*--------------------------------------------------------------------------------------------------
|
|
// ____ _ __ __ _____ _ _ _
|
|
// | _ \ _ __ ___ ___ ___| |_| \/ |___ __ _| ___(_) ___| | __| |
|
|
// | |_) | '__/ _ \/ __|/ _ \ __| |\/| / __|/ _` | |_ | |/ _ \ |/ _` |
|
|
// | __/| | | __/\__ \ __/ |_| | | \__ \ (_| | _| | | __/ | (_| |
|
|
// |_| |_| \___||___/\___|\__|_| |_|___/\__, |_| |_|\___|_|\__,_|
|
|
// |___/
|
|
--------------------------------------------------------------------------------------------------*/
|
|
|
|
struct PresetMsgField
|
|
{
|
|
// Methods.
|
|
// ---------------------------------------------------------------------------------------------
|
|
public: PresetMsgField() :
|
|
presenceBitmapOffset(0),
|
|
presenceBitmapSize(0),
|
|
presenceBitValue(0)
|
|
{
|
|
}
|
|
|
|
// Members.
|
|
// ---------------------------------------------------------------------------------------------
|
|
public: FieldType type;
|
|
public: size_t size;
|
|
public: size_t offset;
|
|
public: size_t presenceBitmapOffset;
|
|
public: size_t presenceBitmapSize;
|
|
public: uint64_t presenceBitValue;
|
|
public: union
|
|
{
|
|
struct // For FieldScaleInteger and FieldScaleUinteger.
|
|
{
|
|
uint32_t factorSize;
|
|
uint32_t factor;
|
|
};
|
|
char stringsListLoc[sizeof(baseutils::Allocator::Loc)]; // For FieldStringFromList (using Allocator::Loc directly is impossible because constructors are prohibited in unions).
|
|
uint32_t orderIdSize; // For FieldGxaOrderId and FieldGxaOrderIdPtr.
|
|
};
|
|
};
|
|
|
|
/*--------------------------------------------------------------------------------------------------
|
|
// ____ _ __ __ _ _ _
|
|
// | _ \ _ __ ___ ___ ___| |_| \/ |___ __ _| | | | __| |_ __
|
|
// | |_) | '__/ _ \/ __|/ _ \ __| |\/| / __|/ _` | |_| |/ _` | '__|
|
|
// | __/| | | __/\__ \ __/ |_| | | \__ \ (_| | _ | (_| | |
|
|
// |_| |_| \___||___/\___|\__|_| |_|___/\__, |_| |_|\__,_|_|
|
|
// |___/
|
|
--------------------------------------------------------------------------------------------------*/
|
|
|
|
struct PresetMsgHdr
|
|
{
|
|
// Methods.
|
|
// ---------------------------------------------------------------------------------------------
|
|
public: PresetMsgHdr(PresetMsgLoc p_next=PresetMsgLoc()) :
|
|
next(p_next)
|
|
{
|
|
}
|
|
|
|
// Members.
|
|
// ---------------------------------------------------------------------------------------------
|
|
public: PresetMsgLoc next; // Location of the next preset message.
|
|
public: size_t size; // The number of bytes needed to store this preset message.
|
|
public: size_t nbParams; // Number of parameters to read in the constructDataForPresetMsg method.
|
|
public: size_t paramsSize; // Number of bytes needed to store the variable parameters.
|
|
public: size_t nbFields; // Number of fields.
|
|
public: size_t fieldsOffset; // Offset of the 'PresetMsgField' members.
|
|
public: size_t nbParts; // Number of parts.
|
|
public: size_t partsOffset; // Offset of the 'parts' member.
|
|
public: size_t constStringsOffset; // Offset of 'constStrings' member.
|
|
public: PresetMsgParam params[0];
|
|
|
|
};
|
|
|
|
/*--------------------------------------------------------------------------------------------------
|
|
// ____ _ _
|
|
// / ___| |_ _| | ___ __ _
|
|
// | | | \ \/ / | / _ \ / _` |
|
|
// | |___| |> <| |__| (_) | (_| |
|
|
// \____|_/_/\_\_____\___/ \__, |
|
|
// |___/
|
|
--------------------------------------------------------------------------------------------------*/
|
|
|
|
class ClxLog
|
|
{
|
|
// Types
|
|
// -------------------------------------------------------------------------------------
|
|
private: typedef std::vector<PresetMsgField> FieldsList;
|
|
|
|
// Methods
|
|
// -------------------------------------------------------------------------------------
|
|
public: ClxLog();
|
|
public: ~ClxLog();
|
|
|
|
public: static bool initialize(std::string const& p_allocatorName,
|
|
size_t p_bufferSize=20 * 1024,
|
|
char const* p_altFilename="",
|
|
size_t p_allocatorPageSize=defaultAllocatorPageSize);// __attribute__((deprecated));
|
|
public: static bool isInitialized() //__attribute__((deprecated))
|
|
{
|
|
return(not sm_allocatorName.empty());
|
|
}
|
|
|
|
private: bool open(std::string const& p_allocatorName,
|
|
size_t p_allocatorPageSize,
|
|
std::string const& p_altFilename,
|
|
size_t p_bufferSize,
|
|
char const* p_appliName,
|
|
int* p_level,
|
|
bool p_keepLevelPtr);
|
|
public: bool open(std::string const& p_allocatorName, size_t p_allocatorPageSize, std::string const& p_altFilename, size_t p_bufferSize,
|
|
char const* p_appliName, int p_level)
|
|
{
|
|
return(open(p_allocatorName, p_allocatorPageSize, p_altFilename, p_bufferSize, p_appliName, &p_level, false));
|
|
}
|
|
public: bool open(std::string const& p_allocatorName, std::string const& p_altFilename, size_t p_bufferSize,
|
|
char const* p_appliName, int p_level)
|
|
{
|
|
return(open(p_allocatorName, defaultAllocatorPageSize, p_altFilename, p_bufferSize, p_appliName, &p_level, false));
|
|
}
|
|
public: bool open(std::string const& p_allocatorName, size_t p_allocatorPageSize, std::string const& p_altFilename, size_t p_bufferSize,
|
|
char const* p_appliName, int* p_level)
|
|
{
|
|
return(open(p_allocatorName, p_allocatorPageSize, p_altFilename, p_bufferSize, p_appliName, p_level, true));
|
|
}
|
|
public: bool open(char const* p_appliName, int p_level=LOG_NOTICE)
|
|
{
|
|
return(open(sm_allocatorName, sm_allocatorPageSize, sm_altFile, sm_bufferSize, p_appliName, &p_level, false));
|
|
}
|
|
public: bool open(char const* p_appliName, int p_level, size_t p_bufferSize)
|
|
{
|
|
return(open(sm_allocatorName, sm_allocatorPageSize, sm_altFile, p_bufferSize, p_appliName, &p_level, false));
|
|
}
|
|
public: bool open(char const* p_appliName, int* p_levelPtr)
|
|
{
|
|
return(open(sm_allocatorName, sm_allocatorPageSize, sm_altFile, sm_bufferSize, p_appliName, p_levelPtr, true));
|
|
}
|
|
public: bool open(std::string const& p_appliName, int p_level=LOG_NOTICE)
|
|
{
|
|
return(open(sm_allocatorName, sm_allocatorPageSize, sm_altFile, sm_bufferSize, p_appliName.c_str(), &p_level, false));
|
|
}
|
|
public: bool open(std::string const& p_appliName, int* p_levelPtr)
|
|
{
|
|
return(open(sm_allocatorName, sm_allocatorPageSize, sm_altFile, sm_bufferSize, p_appliName.c_str(), p_levelPtr, true));
|
|
}
|
|
public: bool isOpened() const
|
|
{
|
|
return(m_allocator != nullptr);
|
|
}
|
|
public: bool isClosed() const
|
|
{
|
|
return(m_allocator == nullptr);
|
|
}
|
|
public: bool isConnected() const;
|
|
public: static bool isDaemonRunning(std::string const& p_allocatorName);
|
|
|
|
public: void refreshPid();
|
|
private: static int openAlternateFile(std::string const& p_filename);
|
|
private: bool createHeader(std::string const& p_altFilename);
|
|
public: void close();
|
|
public: static void freeResources(baseutils::Allocator* p_allocator, std::string const& p_name);
|
|
private: static void freeResources(baseutils::Allocator* p_allocator, ClxLogHeader*& pio_header);
|
|
public: void debugMode(bool p_enabled=true) { m_debugMode = p_enabled; }
|
|
private: static void onThreadDestroyed(void* p_writer);
|
|
private: Writer* getWriter();
|
|
public: bool connectToServer();
|
|
|
|
public: baseutils::Allocator const* allocator() const { return(m_allocator); }
|
|
|
|
public: void changeLevel(int p_newLevel)
|
|
{
|
|
m_levelStorage = p_newLevel;
|
|
__sync_synchronize();
|
|
}
|
|
public: void changeLevelPtr(int* p_newLevelAddress)
|
|
{
|
|
m_level = p_newLevelAddress;
|
|
__sync_synchronize();
|
|
}
|
|
public: char const* appliName() const;
|
|
public: bool changeAppliName(char const* p_appliName);
|
|
public: bool changeAppliName(std::string const& p_appliName)
|
|
{
|
|
return(changeAppliName(p_appliName.c_str()));
|
|
}
|
|
|
|
// Accessors.
|
|
public: int level() { return(*m_level); }
|
|
public: char const* internalName() { return(m_header ? m_header->name : ""); }
|
|
|
|
// Log methods.
|
|
public: void semergency(char const* p_format) { strncpy(LOG_EMERGENCY, p_format); }
|
|
public: void salert(char const* p_format) { strncpy(LOG_ALERT, p_format); }
|
|
public: void scritical(char const* p_format) { strncpy(LOG_CRITICAL, p_format); }
|
|
public: void serror(char const* p_format) { strncpy(LOG_ERROR, p_format); }
|
|
public: void swarning(char const* p_format) { strncpy(LOG_WARNING, p_format); }
|
|
public: void snotice(char const* p_format) { strncpy(LOG_NOTICE, p_format); }
|
|
public: void sinfo(char const* p_format) { if(*m_level >= LOG_INFO) { strncpy(LOG_INFO, p_format); } }
|
|
public: void sdebug(char const* p_format) { if(*m_level >= LOG_DEBUG) { strncpy(LOG_DEBUG, p_format); } }
|
|
public: void emergency(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void alert(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void critical(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void error(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void warning(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void notice(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void info(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void debug(char const* p_format, ...) __attribute__ ((format (printf, 2, 3)));
|
|
public: void forcePrint(int p_level, char const* p_format, va_list p_args) { internalPrint(p_level, p_format, p_args); }
|
|
public: void forcePrint(int p_level, char const* p_format, ...) __attribute__ ((format (printf, 3, 4)));
|
|
public: void print(int p_level, char const* p_format, ...) __attribute__ ((format (printf, 3, 4)));
|
|
public: void print(int p_level, char const* p_format, va_list p_args) { if(p_level <= *m_level) internalPrint(p_level, p_format, p_args); }
|
|
private: void internalPrint(int p_level, char const* p_format, va_list p_args);
|
|
public: void internalPrint(int p_level, char const* p_format, ...) __attribute__ ((format (printf, 3, 4)));
|
|
public: void strncpy(int p_level, char const* p_msg);
|
|
public: void strcpy(int p_level, char const* p_msg);
|
|
public: void memcpy(int p_level, char const* p_msg, size_t p_len);
|
|
public: bool presetMsg(ObjectId const& p_id, int p_level, va_list p_args) { if(p_level <= *m_level) return(internalPresetMsg(p_id, p_level, p_args)); return(true); }
|
|
public: bool presetMsg(ObjectId const& p_id, int p_level, ...);
|
|
private: bool internalPresetMsg(ObjectId const& p_id, int p_level, va_list p_args);
|
|
private: void writeInFile(int p_level, char* p_msg, size_t p_len);
|
|
private: void writePresetMsgInFile(int p_level, baseutils::Allocator::Loc const& p_loc, va_list p_args);
|
|
|
|
// Convenients methods.
|
|
public: void emergency(std::string const& p_text) { strncpy(LOG_EMERGENCY, p_text.c_str()); }
|
|
public: void alert(std::string const& p_text) { strncpy(LOG_ALERT, p_text.c_str()); }
|
|
public: void critical(std::string const& p_text) { strncpy(LOG_CRITICAL, p_text.c_str()); }
|
|
public: void error(std::string const& p_text) { strncpy(LOG_ERROR, p_text.c_str()); }
|
|
public: void warning(std::string const& p_text) { strncpy(LOG_WARNING, p_text.c_str()); }
|
|
public: void notice(std::string const& p_text) { strncpy(LOG_NOTICE, p_text.c_str()); }
|
|
public: void info(std::string const& p_text) { if(*m_level >= LOG_INFO) { strncpy(LOG_INFO, p_text.c_str()); } }
|
|
public: void debug(std::string const& p_text) { if(*m_level >= LOG_DEBUG) { strncpy(LOG_DEBUG, p_text.c_str()); } }
|
|
|
|
public: static char const* getError()
|
|
{
|
|
return(tls_error);
|
|
}
|
|
|
|
// Methods linked to the strings list.
|
|
public: ObjectId addStringsList(char const* p_defaultText, va_list p_args);
|
|
public: ObjectId addStringsList(char const* p_defaultText, ...);
|
|
private: void printStringList(ObjectId const& p_id);
|
|
private: static void destroyStringsLists(baseutils::Allocator* p_allocator, ClxLogHeader* p_header);
|
|
|
|
// Methods linked to the preset messages.
|
|
public: ObjectId addPresetMsg(char const* p_format, va_list p_args);
|
|
public: ObjectId addPresetMsg(char const* p_format, ...);
|
|
private: char const* addStringFromListParam(PresetMsgParam& pio_param, FieldsList& po_fields, size_t& pio_offset, va_list p_args);
|
|
private: char const* addStringParam(PresetMsgParam& pio_param, FieldsList& po_fields, size_t& pio_offset, va_list p_args);
|
|
private: char const* addIntegerParam(PresetMsgParam& pio_param, FieldsList& po_fields, size_t& pio_offset, bool p_unsigned, va_list p_args);
|
|
private: char const* addScaleIntegerParam(PresetMsgParam& pio_param, FieldsList& po_fields, size_t& pio_offset, bool p_unsigned, va_list p_args);
|
|
private: char const* addCharParam(PresetMsgParam& pio_param, FieldsList& po_fields, size_t& pio_offset);
|
|
// private: char const* addGxaOrderIdField(PresetMsgParam& pio_param);
|
|
private: char const* addPointerParam(PresetMsgParam& pio_param, FieldsList& po_fields, size_t& pio_offset);
|
|
private: char const* addStructParam(PresetMsgParam& pio_param, FieldsList& po_fields, size_t& pio_offset, std::set<PresetMsgParam>& po_params, va_list p_args);
|
|
public: void printPresetMsgDetails(ObjectId const& p_id);
|
|
private: static void destroyPresetMsgs(baseutils::Allocator* p_allocator, ClxLogHeader* p_header);
|
|
|
|
// Constants.
|
|
// -------------------------------------------------------------------------------------
|
|
// These members are needed to manage the variables linked to a specific thread.
|
|
public: static size_t const defaultAllocatorPageSize = 50 * 1024 * 1024;
|
|
public: static size_t const minMsgBufferSize = 20 * 1024;
|
|
public: static size_t const maxMsgBufferSize = 500 * 1024 * 1024;
|
|
public: static size_t const maxMsgSize = 2000;
|
|
private: static size_t const presetMsgNewlinePart = ~0ULL;
|
|
|
|
// Static members
|
|
// -------------------------------------------------------------------------------------
|
|
// These members can be set with the initialize method. They are default values for the open methods.
|
|
private: static std::string sm_allocatorName;
|
|
private: static std::string sm_altFile;
|
|
private: static size_t sm_bufferSize; ///@brief The size of the buffer allocated for a thread to store logs.
|
|
private: static size_t sm_allocatorPageSize; ///@brief The page size of the allocator (used if the allocator doesn't exist when the client starts).
|
|
|
|
public: static int logLevelFromString(std::string const& str);
|
|
|
|
// Members
|
|
// -------------------------------------------------------------------------------------
|
|
private: baseutils::Allocator* m_allocator;
|
|
private: baseutils::Mutex* m_serverMutex;
|
|
private: int* m_level;
|
|
private: ClxLogHeader* m_header;
|
|
private: Writer* m_firstWriter;
|
|
private: PrintableMsg* m_printableMsgArray;
|
|
private: uint64_t m_creationTime;
|
|
private: size_t m_printableMsgArraySize;
|
|
private: std::string m_allocatorName;
|
|
private: int m_alternateFile;
|
|
private: int m_levelStorage;
|
|
private: size_t m_bufferSize;
|
|
private: AppliName m_appliName;
|
|
private: baseutils::Mutex m_mutex;
|
|
private: bool m_debugMode;
|
|
|
|
friend class Writer;
|
|
friend class Reader;
|
|
friend class Client;
|
|
};
|
|
|
|
# define ldebug(l, fmt, ...) { if((l).level() >= LOG_DEBUG) { (l).internalPrint(LOG_DEBUG, fmt, ## __VA_ARGS__); } }
|
|
# define linfo(l, fmt, ...) { if((l).level() >= LOG_INFO) { (l).internalPrint(LOG_INFO, fmt, ## __VA_ARGS__); } }
|
|
}
|