提交 09eadf5c 编写于 作者: E Enrico Giordani

[Code cleanup] Event log code refactoring. Code formatting.

Rollbacked setLogFile method name.
上级 d9369130
......@@ -32,6 +32,13 @@ using namespace std;
#include "Win32_SmartHandle.h"
#include "EventLog.h"
static bool eventLogEnabled = true;
static string eventLogIdentity = "redis";
void RedisEventLog::SetEventLogIdentity(const char* identity) {
eventLogIdentity = string(identity);
}
void RedisEventLog::UninstallEventLogSource() {
SmartRegistryHandle appKey;
if (ERROR_SUCCESS == RegOpenKeyA(HKEY_LOCAL_MACHINE, cEventLogApplicitonPath.c_str(), appKey)) {
......@@ -82,20 +89,20 @@ void RedisEventLog::InstallEventLogSource(string appPath) {
DWORD type = REG_DWORD;
DWORD size = sizeof(DWORD);
if (ERROR_SUCCESS != RegQueryValueExA(redisserver, cTypesSupported.c_str(), 0, &type, NULL, &size)) {
if (ERROR_SUCCESS != RegSetValueExA(redisserver, cTypesSupported.c_str(), 0, REG_DWORD, (const BYTE*)&value, sizeof(DWORD))) {
if (ERROR_SUCCESS != RegSetValueExA(redisserver, cTypesSupported.c_str(), 0, REG_DWORD, (const BYTE*) &value, sizeof(DWORD))) {
throw std::system_error(GetLastError(), system_category(), "RegSetValueExA failed");
}
}
type = REG_SZ;
size = 0;
if (ERROR_SUCCESS != RegQueryValueExA(redisserver, cEventMessageFile.c_str(), 0, &type, NULL, &size)) {
if (ERROR_SUCCESS != RegSetValueExA(redisserver, cEventMessageFile.c_str(), 0, REG_SZ, (BYTE*)appPath.c_str(), (DWORD)appPath.length())) {
if (ERROR_SUCCESS != RegSetValueExA(redisserver, cEventMessageFile.c_str(), 0, REG_SZ, (BYTE*) appPath.c_str(), (DWORD) appPath.length())) {
throw std::system_error(GetLastError(), system_category(), "RegSetValueExA failed");
}
}
SmartRegistryHandle application;
if (ERROR_SUCCESS != RegOpenKeyA(eventLogKey, cApplication.c_str() , application)) {
if (ERROR_SUCCESS != RegOpenKeyA(eventLogKey, cApplication.c_str(), application)) {
throw std::system_error(GetLastError(), system_category(), "RegCreateKeyA failed");
}
SmartRegistryHandle redis2;
......@@ -107,18 +114,18 @@ void RedisEventLog::InstallEventLogSource(string appPath) {
type = REG_DWORD;
size = 0;
if (ERROR_SUCCESS != RegQueryValueExA(redis2, cTypesSupported.c_str(), 0, &type, NULL, &size)) {
if (ERROR_SUCCESS != RegSetValueExA(redis2, cTypesSupported.c_str(), 0, REG_DWORD, (const BYTE*)&value, sizeof(DWORD))) {
if (ERROR_SUCCESS != RegSetValueExA(redis2, cTypesSupported.c_str(), 0, REG_DWORD, (const BYTE*) &value, sizeof(DWORD))) {
throw std::system_error(GetLastError(), system_category(), "RegSetValueExA failed");
}
}
if (ERROR_SUCCESS != RegQueryValueExA(redis2, cEventMessageFile.c_str(), 0, &type, NULL, &size)) {
if (ERROR_SUCCESS != RegSetValueExA(redis2, cEventMessageFile.c_str(), 0, REG_SZ, (BYTE*)appPath.c_str(), (DWORD)appPath.length())) {
if (ERROR_SUCCESS != RegSetValueExA(redis2, cEventMessageFile.c_str(), 0, REG_SZ, (BYTE*) appPath.c_str(), (DWORD) appPath.length())) {
throw std::system_error(GetLastError(), system_category(), "RegSetValueExA failed");
}
}
}
void RedisEventLog::LogMessageToEventLog(LPCSTR msg, const WORD type) {
void RedisEventLog::LogMessage(LPCSTR msg, const WORD type) {
DWORD eventID;
switch (type) {
case EVENTLOG_ERROR_TYPE:
......@@ -141,7 +148,7 @@ void RedisEventLog::LogMessageToEventLog(LPCSTR msg, const WORD type) {
if (0 == hEventLog) {
std::cerr << "Failed open source '" << this->eventLogName << "': " << GetLastError() << endl;
} else {
if (FALSE == ReportEventA( hEventLog, type, 0, eventID, 0, 1, 0, &msg, 0)) {
if (FALSE == ReportEventA(hEventLog, type, 0, eventID, 0, 1, 0, &msg, 0)) {
std::cerr << "Failed to write message: " << GetLastError() << endl;
}
......@@ -149,12 +156,65 @@ void RedisEventLog::LogMessageToEventLog(LPCSTR msg, const WORD type) {
}
}
extern "C" void WriteEventLog(const char* sysLogInstance, const char* msg) {
void RedisEventLog::LogError(string msg) {
try {
if (eventLogEnabled == true) {
stringstream ss;
ss << "syslog-ident = " << eventLogIdentity << endl;
ss << msg;
RedisEventLog().LogMessage(ss.str().c_str(), EVENTLOG_ERROR_TYPE);
}
}
catch (...) {
}
}
string RedisEventLog::GetEventLogIdentity() {
return eventLogIdentity;
}
void RedisEventLog::EnableEventLog(bool enabled) {
eventLogEnabled = enabled;
}
bool RedisEventLog::IsEventLogEnabled() {
return eventLogEnabled;
}
extern "C" void setSyslogEnabled(int enabled) {
try {
if (enabled == 1) {
RedisEventLog().EnableEventLog(true);
} else {
RedisEventLog().EnableEventLog(false);
}
}
catch (...) {}
}
extern "C" void setSyslogIdent(char* identity) {
try {
RedisEventLog().SetEventLogIdentity(identity);
}
catch (...) {}
}
extern "C" void WriteEventLog(const char* msg) {
try {
stringstream ss;
ss << "syslog-ident = " << sysLogInstance << endl;
ss << "syslog-ident = " << RedisEventLog().GetEventLogIdentity() << endl;
ss << msg;
RedisEventLog().LogMessageToEventLog(ss.str().c_str(),EVENTLOG_INFORMATION_TYPE);
} catch (...) {
RedisEventLog().LogMessage(ss.str().c_str(), EVENTLOG_INFORMATION_TYPE);
}
catch (...) {}
}
extern "C" int IsEventLogEnabled() {
try {
if (RedisEventLog().IsEventLogEnabled() == true) {
return 1;
}
}
catch (...) {}
return 0;
}
......@@ -33,10 +33,16 @@ public:
void InstallEventLogSource(string appPath);
void UninstallEventLogSource();
void LogMessageToEventLog(LPCSTR msg, const WORD type);
void SetEventLogIdentity(const char* identity);
private:
void LogMessage(LPCSTR msg, const WORD type);
void LogError(string msg);
string GetEventLogIdentity();
void EnableEventLog(bool enabled);
bool IsEventLogEnabled();
private:
const string eventLogName = "redis";
const string cEventLogPath = "SYSTEM\\CurrentControlSet\\Services\\EventLog\\";
const string cEventLogApplicitonPath = cEventLogPath + "Application\\";
......@@ -50,7 +56,10 @@ private:
extern "C" {
#endif
void WriteEventLog(const char* sysLogInstance, const char* msg);
void setSyslogEnabled(int enabled);
void setSyslogIdent(char* identity);
int IsEventLogEnabled();
void WriteEventLog(const char* msg);
#ifdef __cplusplus
}
......
......@@ -105,6 +105,7 @@ allocate a system paging file that will expand up to about (3.5 * physical).
#include "Win32_RedisLog.h"
#include "Win32_StackTrace.h"
#include "Win32_ThreadControl.h"
#include "Win32_EventLog.h"
#ifdef USE_DLMALLOC
#include "Win32_dlmalloc.h"
......@@ -1110,11 +1111,11 @@ void SetupLogging() {
string syslogIdent = (g_argMap.find(cSyslogIdent) != g_argMap.end() ? g_argMap[cSyslogIdent].at(0).at(0) : cDefaultSyslogIdent);
string logFileName = (g_argMap.find(cLogfile) != g_argMap.end() ? g_argMap[cLogfile].at(0).at(0) : cDefaultLogfile);
setSyslogEnabled(syslogEnabled);
RedisEventLog().EnableEventLog(syslogEnabled);
if (syslogEnabled) {
setSyslogIdent(syslogIdent.c_str());
RedisEventLog().SetEventLogIdentity(syslogIdent.c_str());
} else {
setLogFilename(logFileName.c_str());
setLogFile(logFileName.c_str());
}
}
......
......@@ -36,20 +36,10 @@
static const char ellipsis[] = "[...]";
static const char ellipsisWithNewLine[] = "[...]\n";
static int verbosity = REDIS_WARNING;
static int syslogEnabled = 0;
static char syslogIdent[MAX_PATH];
static HANDLE hLogFile = INVALID_HANDLE_VALUE;
static int isStdout = 0;
static char* logFilename = NULL;
void setSyslogEnabled(int flag) {
syslogEnabled = flag;
}
void setSyslogIdent(const char* ident) {
strcpy_s(syslogIdent, MAX_PATH, ident);
}
void setLogVerbosityLevel(int level)
{
verbosity = level;
......@@ -64,9 +54,10 @@ const char* getLogFilename() {
}
/* We keep the file handle open to improve performance.
* This assumes that calls to redisLog and setLogFile will not happen concurrently.
*/
void setLogFilename(const char* filename)
* This assumes that calls to redisLog and setLogFile will not happen
* concurrently.
*/
void setLogFile(const char* filename)
{
if (logFilename != NULL) {
free((void*) logFilename);
......@@ -187,7 +178,9 @@ void redisLogRaw(int level, const char *msg) {
FlushFileBuffers(hLogFile);
#endif
if (syslogEnabled) WriteEventLog(syslogIdent, msg);
if (IsEventLogEnabled() == 1) {
WriteEventLog(msg);
}
}
/* Like redisLogRaw() but with printf-alike support. This is the function that
......
......@@ -39,10 +39,8 @@
extern "C" {
#endif
void setSyslogEnabled(int flag);
void setSyslogIdent(const char* ident);
void setLogVerbosityLevel(int level);
void setLogFilename(const char* filename);
void setLogFile(const char* filename);
const char* getLogFilename();
void redisLogRaw(int level, const char *msg);
void redisLog(int level, const char *fmt, ...);
......
......@@ -30,6 +30,7 @@
#ifdef _WIN32
#include "Win32_Interop/win32_types.h"
#include "Win32_Interop/Win32_EventLog.h"
#include <direct.h>
#endif
......@@ -217,7 +218,7 @@ void loadServerConfigFromString(char *config) {
goto loaderr;
#ifdef _WIN32
} else {
setLogFilename(server.logfile);
setLogFile(server.logfile);
#endif
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册