主要用于限制一下mangos服务端的日志文件server.log 的大小。当大小超过以后,就另外新建立一个文件
From 8704557095df5f389144774447a1611bf82a00c8 Mon Sep 17 00:00:00 2001From: unknown <Matthijs@.FrogMu.fragfrog.nl>
Date: Tue, 15 Dec 2009 02:33:36 +0100
Subject: [PATCH] Limit log filesize
---
src/mangosd/mangosd.conf.dist.in | 8 ++++++-
src/shared/Log.cpp | 42 ++++++++++++++++++++++++++++++++++++++
src/shared/Log.h | 7 ++++++
3 files changed, 56 insertions(+), 1 deletions(-)
diff --git a/src/mangosd/mangosd.conf.dist.in b/src/mangosd/mangosd.conf.dist.in
index 522bc16..ffc1232 100644
--- a/src/mangosd/mangosd.conf.dist.in
+++ b/src/mangosd/mangosd.conf.dist.in
@@ -1,4 +1,4 @@
-#####################################
+#####################################
# MaNGOS Configuration file #
#####################################
ConfVersion=2008080101
@@ -223,6 +223,11 @@ AddonChannel = 1
# 0 = Minimum; 1 = Error; 2 = Detail; 3 = Full/Debug
# Default: 0
#
+# LogFileSize
+# Server logging file size
+# 0 = disabled
+# Default: 0
+#
# LogFilter_TransportMoves
# LogFilter_CreatureMoves
# LogFilter_VisibilityChanges
@@ -292,6 +297,7 @@ LogTime = 0
LogFile = "Server.log"
LogTimestamp = 0
LogFileLevel = 0
+LogFileSize = 0
LogFilter_TransportMoves = 1
LogFilter_CreatureMoves = 1
LogFilter_VisibilityChanges = 1
diff --git a/src/shared/Log.cpp b/src/shared/Log.cpp
index 72544d4..8a983ca 100644
--- a/src/shared/Log.cpp
+++ b/src/shared/Log.cpp
@@ -246,6 +246,10 @@ void Log::Initialize()
// Char log settings
m_charLog_Dump = sConfig.GetBoolDefault("CharLogDump", false);
+
+ // Log File size
+ m_count_lines = 0;
+ m_file_size_limit = sConfig.GetIntDefault("LogFileSize", 0);
}
FILE* Log::openLogFile(char const* configFileName,char const* configTimeStampFlag, char const* mode)
@@ -262,6 +266,10 @@ FILE* Log::openLogFile(char const* configFileName,char const* configTimeStampFla
else
logfn += m_logsTimestamp;
}
+ if(strcmp(configFileName,"LogFile") == 0 && log_filename.empty())
+ {
+ log_filename=log_filename.append(m_logsDir+logfn);
+ }
return fopen((m_logsDir+logfn).c_str(), mode);
}
@@ -337,6 +345,7 @@ void Log::outTitle( const char * str)
fprintf(logfile, str);
fprintf(logfile, " " );
fflush(logfile);
+ swapLogFile();
}
fflush(stdout);
@@ -352,6 +361,7 @@ void Log::outString()
outTimestamp(logfile);
fprintf(logfile, " " );
fflush(logfile);
+ swapLogFile();
}
fflush(stdout);
}
@@ -387,6 +397,7 @@ void Log::outString( const char * str, ... )
va_end(ap);
fflush(logfile);
+ swapLogFile();
}
fflush(stdout);
}
@@ -423,6 +434,7 @@ void Log::outError( const char * err, ... )
fprintf(logfile, " " );
fflush(logfile);
+ swapLogFile();
}
fflush(stderr);
}
@@ -460,6 +472,7 @@ void Log::outErrorDb( const char * err, ... )
fprintf(logfile, " " );
fflush(logfile);
+ swapLogFile();
}
if(dberLogfile)
@@ -510,6 +523,7 @@ void Log::outBasic( const char * str, ... )
fprintf(logfile, " " );
va_end(ap);
fflush(logfile);
+ swapLogFile();
}
fflush(stdout);
}
@@ -549,6 +563,7 @@ void Log::outDetail( const char * str, ... )
fprintf(logfile, " " );
fflush(logfile);
+ swapLogFile();
}
fflush(stdout);
@@ -613,6 +628,7 @@ void Log::outDebug( const char * str, ... )
fprintf(logfile, " " );
fflush(logfile);
+ swapLogFile();
}
fflush(stdout);
}
@@ -649,6 +665,7 @@ void Log::outCommand( uint32 account, const char * str, ... )
fprintf(logfile, " " );
va_end(ap);
fflush(logfile);
+ swapLogFile();
}
if (m_gmlog_per_account)
@@ -733,6 +750,7 @@ void Log::outMenu( const char * str, ... )
fprintf(logfile, " " );
fflush(logfile);
+ swapLogFile();
}
fflush(stdout);
}
@@ -754,6 +772,30 @@ void Log::outRALog( const char * str, ... )
fflush(stdout);
}
+void Log::swapLogFile()
+{
+ if (logfile && m_file_size_limit>0) // there is a logfile and a size limit
+ {
+ if(m_count_lines >= m_file_size_limit)
+ {
+ m_count_lines=0;
+ if(log_size_limit_filename.empty()) // generate backup file name if empty
+ {
+ size_t dot_pos = log_filename.find_last_of(".");
+ log_size_limit_filename.append(log_filename);
+ log_size_limit_filename.insert(dot_pos,"_part");
+ }
+ // close the logfile, remove, rename, open new logfile .. TODO Error Handling
+ fclose(logfile);
+ remove(log_size_limit_filename.c_str());
+ rename(log_filename.c_str(),log_size_limit_filename.c_str());
+ logfile=fopen(log_filename.c_str(), "w");
+ }
+ else {m_count_lines++;}
+ }
+}
+
+
void outstring_log(const char * str, ...)
{
if( !str )
diff --git a/src/shared/Log.h b/src/shared/Log.h
index 27be84f..f62579e 100644
--- a/src/shared/Log.h
+++ b/src/shared/Log.h
@@ -146,6 +146,13 @@ class Log : public MaNGOS::Singleton<Log, MaNGOS::ClassLevelLockable<Log, ACE_Th
// gm log control
bool m_gmlog_per_account;
std::string m_gmlog_filename_format;
+
+ // log file size limit
+ uint32 m_file_size_limit; // limit size <= 0 deaktivate
+ uint32 m_count_lines; // count the lines in the log file
+ std::string log_filename;
+ std::string log_size_limit_filename;
+ void swapLogFile();
};
#define sLog MaNGOS::Singleton<Log>::Instance()
--
1.6.1.9.g97c34
|