stoneharry

Untitled

Oct 10th, 2011
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.28 KB | None | 0 0
  1. /*
  2.  * ArcEmu MMORPG Server
  3.  * Copyright (C) 2008-2011 <http://www.ArcEmu.org/>
  4.  *
  5.  * This program is free software: you can redistribute it and/or modify
  6.  * it under the terms of the GNU Affero General Public License as published by
  7.  * the Free Software Foundation, either version 3 of the License, or
  8.  * any later version.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU Affero General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU Affero General Public License
  16.  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17.  *
  18.  */
  19.  
  20. #include "Config/ConfigEnv.h"
  21. #include "Log.h"
  22. #include <cstdarg>
  23.  
  24. string FormatOutputString(const char* Prefix, const char* Description, bool useTimeStamp)
  25. {
  26.  
  27.     char p[MAX_PATH];
  28.     p[0] = 0;
  29.     time_t t = time(NULL);
  30.     tm* a = gmtime(&t);
  31.     strcat(p, Prefix);
  32.     strcat(p, "/");
  33.     strcat(p, Description);
  34.     if(useTimeStamp)
  35.     {
  36.         char ftime[100];
  37.         snprintf(ftime, 100, "-%-4d-%02d-%02d %02d-%02d-%02d", a->tm_year + 1900, a->tm_mon + 1, a->tm_mday, a->tm_hour, a->tm_min, a->tm_sec);
  38.         strcat(p, ftime);
  39.     }
  40.  
  41.     strcat(p, ".log");
  42.     return string(p);
  43. }
  44.  
  45. createFileSingleton(oLog);
  46. initialiseSingleton(WorldLog);
  47.  
  48. SERVER_DECL time_t UNIXTIME;
  49. SERVER_DECL tm g_localTime;
  50.  
  51. void oLog::outFile(FILE* file, char* msg, const char* source)
  52. {
  53.     char time_buffer[TIME_FORMAT_LENGTH];
  54.     char szltr_buffer[SZLTR_LENGTH];
  55.     Time(time_buffer);
  56.     pdcds(SZLTR, szltr_buffer);
  57.  
  58.     if(source != NULL)
  59.     {
  60.         fprintf(file, "%s%s%s: %s\n", time_buffer, szltr_buffer, source, msg);
  61.         printf("%s%s%s: %s\n", time_buffer, szltr_buffer, source, msg);
  62.     }
  63.     else
  64.     {
  65.         fprintf(file, "%s%s%s\n", time_buffer, szltr_buffer, msg);
  66.         printf("%s%s%s\n", time_buffer, szltr_buffer, msg);
  67.     }
  68. }
  69.  
  70. void oLog::Time(char* buffer)
  71. {
  72.     time_t now;
  73.     struct tm* timeinfo = NULL;
  74.  
  75.     time(&now);
  76.     timeinfo = localtime(&now);
  77.  
  78.     if(timeinfo != NULL)
  79.     {
  80.         strftime(buffer, TIME_FORMAT_LENGTH, TIME_FORMAT, timeinfo);
  81.     }
  82.     else
  83.     {
  84.         buffer[0] = '\0';
  85.     }
  86. }
  87.  
  88. void oLog::outString(const char* str, ...)
  89. {
  90.     if(m_normalFile == NULL)
  91.         return;
  92.  
  93.     char buf[32768];
  94.     va_list ap;
  95.  
  96.     va_start(ap, str);
  97.     vsnprintf(buf, 32768, str, ap);
  98.     va_end(ap);
  99.  
  100.     outFile(m_normalFile, buf);
  101. }
  102.  
  103. void oLog::outError(const char* err, ...)
  104. {
  105.     if(m_errorFile == NULL)
  106.         return;
  107.  
  108.     char buf[32768];
  109.     va_list ap;
  110.  
  111.     va_start(ap, err);
  112.     vsnprintf(buf, 32768, err, ap);
  113.     va_end(ap);
  114.  
  115.     outFile(m_errorFile, buf);
  116. }
  117.  
  118. void oLog::outBasic(const char* str, ...)
  119. {
  120.     if(m_normalFile == NULL)
  121.         return;
  122.  
  123.     char buf[32768];
  124.     va_list ap;
  125.  
  126.     va_start(ap, str);
  127.     vsnprintf(buf, 32768, str, ap);
  128.     va_end(ap);
  129.  
  130.     outFile(m_normalFile, buf);
  131. }
  132.  
  133. void oLog::outDetail(const char* str, ...)
  134. {
  135.     if(m_fileLogLevel < 1 || m_normalFile == NULL)
  136.         return;
  137.  
  138.     char buf[32768];
  139.     va_list ap;
  140.  
  141.     va_start(ap, str);
  142.     vsnprintf(buf, 32768, str, ap);
  143.     va_end(ap);
  144.  
  145.     outFile(m_normalFile, buf);
  146. }
  147.  
  148. void oLog::outDebug(const char* str, ...)
  149. {
  150.     if(m_fileLogLevel < 2 || m_errorFile == NULL)
  151.         return;
  152.  
  153.     char buf[32768];
  154.     va_list ap;
  155.  
  156.     va_start(ap, str);
  157.     vsnprintf(buf, 32768, str, ap);
  158.     va_end(ap);
  159.  
  160.     outFile(m_errorFile, buf);
  161. }
  162.  
  163. void oLog::logBasic(const char* file, int line, const char* fncname, const char* msg, ...)
  164. {
  165.     if(m_normalFile == NULL)
  166.         return;
  167.  
  168.     char buf[ 32768 ];
  169.     char message[ 32768 ];
  170.  
  171.     snprintf(message, 32768, " [BSC] %s:%d %s %s", file, line, fncname, msg);
  172.     va_list ap;
  173.  
  174.     va_start(ap, msg);
  175.     vsnprintf(buf, 32768, message, ap);
  176.     va_end(ap);
  177.  
  178.     outFile(m_normalFile, buf);
  179. }
  180.  
  181. void oLog::logDetail(const char* file, int line, const char* fncname, const char* msg, ...)
  182. {
  183.     if((m_fileLogLevel < 1) || (m_normalFile == NULL))
  184.         return;
  185.  
  186.     char buf[ 32768 ];
  187.     char message[ 32768 ];
  188.  
  189.     snprintf(message, 32768, " [DTL] %s:%d %s %s", file, line, fncname, msg);
  190.     va_list ap;
  191.  
  192.     va_start(ap, msg);
  193.     vsnprintf(buf, 32768, message, ap);
  194.     va_end(ap);
  195.  
  196.     outFile(m_normalFile, buf);
  197. }
  198.  
  199. void oLog::logError(const char* file, int line, const char* fncname, const char* msg, ...)
  200. {
  201.     if(m_errorFile == NULL)
  202.         return;
  203.  
  204.     char buf[ 32768 ];
  205.     char message[ 32768 ];
  206.  
  207.     snprintf(message, 32768, " [ERR] %s:%d %s %s", file, line, fncname, msg);
  208.     va_list ap;
  209.  
  210.     va_start(ap, msg);
  211.     vsnprintf(buf, 32768, message, ap);
  212.     va_end(ap);
  213.  
  214.     outFile(m_errorFile, buf);
  215. }
  216.  
  217. void oLog::logDebug(const char* file, int line, const char* fncname, const char* msg, ...)
  218. {
  219.     if((m_fileLogLevel < 2) || (m_errorFile == NULL))
  220.         return;
  221.  
  222.     char buf[ 32768 ];
  223.     char message[ 32768 ];
  224.  
  225.     snprintf(message, 32768, " [DBG] %s:%d %s %s", file, line, fncname, msg);
  226.     va_list ap;
  227.  
  228.     va_start(ap, msg);
  229.     vsnprintf(buf, 32768, message, ap);
  230.     va_end(ap);
  231.  
  232.     outFile(m_errorFile, buf);
  233. }
  234.  
  235. //old NGLog.h methods
  236. void oLog::Notice(const char* source, const char* format, ...)
  237. {
  238.     if(m_fileLogLevel < 1 || m_normalFile == NULL)
  239.         return;
  240.  
  241.     char buf[32768];
  242.     va_list ap;
  243.  
  244.     va_start(ap, format);
  245.     vsnprintf(buf, 32768, format, ap);
  246.     va_end(ap);
  247.  
  248.     outFile(m_normalFile, buf, source);
  249. }
  250.  
  251. void oLog::Warning(const char* source, const char* format, ...)
  252. {
  253.     if(m_fileLogLevel < 1 || m_normalFile == NULL)
  254.         return;
  255.  
  256.     char buf[32768];
  257.     va_list ap;
  258.  
  259.     va_start(ap, format);
  260.     vsnprintf(buf, 32768, format, ap);
  261.     va_end(ap);
  262.  
  263.     outFile(m_normalFile, buf, source);
  264. }
  265.  
  266. void oLog::Success(const char* source, const char* format, ...)
  267. {
  268.     if(m_normalFile == NULL)
  269.         return;
  270.  
  271.     char buf[32768];
  272.     va_list ap;
  273.  
  274.     va_start(ap, format);
  275.     vsnprintf(buf, 32768, format, ap);
  276.     va_end(ap);
  277.  
  278.     outFile(m_normalFile, buf, source);
  279. }
  280.  
  281. void oLog::Error(const char* source, const char* format, ...)
  282. {
  283.     if(m_errorFile == NULL)
  284.         return;
  285.  
  286.     char buf[32768];
  287.     va_list ap;
  288.  
  289.     va_start(ap, format);
  290.     vsnprintf(buf, 32768, format, ap);
  291.     va_end(ap);
  292.  
  293.     outFile(m_errorFile, buf, source);
  294. }
  295.  
  296. void oLog::Debug(const char* source, const char* format, ...)
  297. {
  298.     if(m_fileLogLevel < 2 || m_errorFile == NULL)
  299.         return;
  300.  
  301.     char buf[32768];
  302.     va_list ap;
  303.  
  304.     va_start(ap, format);
  305.     vsnprintf(buf, 32768, format, ap);
  306.     va_end(ap);
  307.  
  308.     outFile(m_errorFile, buf, source);
  309. }
  310.  
  311. void oLog::LargeErrorMessage(const char* source, ...)
  312. {
  313.     std::vector<char*> lines;
  314.     char* pointer;
  315.     va_list ap;
  316.     va_start(ap, source);
  317.  
  318.     pointer = const_cast<char*>(source);
  319.     lines.push_back(pointer);
  320.  
  321.     size_t i, j, k;
  322.     pointer = va_arg(ap, char*);
  323.     while(pointer != NULL)
  324.     {
  325.         lines.push_back(pointer);
  326.         pointer = va_arg(ap, char*);
  327.     }
  328.  
  329.     outError("*********************************************************************");
  330.     outError("*                        MAJOR ERROR/WARNING                        *");
  331.     outError("*                        ===================                        *");
  332.  
  333.     for(std::vector<char*>::iterator itr = lines.begin(); itr != lines.end(); ++itr)
  334.     {
  335.         stringstream sstext;
  336.         i = strlen(*itr);
  337.         j = (i <= 65) ? 65 - i : 0;
  338.         sstext << "* " << *itr;
  339.         for(k = 0; k < j; ++k)
  340.         {
  341.             sstext << " ";
  342.         }
  343.  
  344.         sstext << " *";
  345.         outError(sstext.str().c_str());
  346.     }
  347.  
  348.     outError("*********************************************************************");
  349. }
  350.  
  351. void oLog::Init(int32 fileLogLevel, LogType logType)
  352. {
  353.     SetFileLoggingLevel(fileLogLevel);
  354.  
  355.     const char* logNormalFilename = NULL, *logErrorFilename = NULL;
  356.     switch(logType)
  357.     {
  358.         case LOGON_LOG:
  359.             {
  360.                 logNormalFilename = "logon-normal.log";
  361.                 logErrorFilename = "logon-error.log";
  362.                 break;
  363.             }
  364.         case WORLD_LOG:
  365.             {
  366.                 logNormalFilename = "world-normal.log";
  367.                 logErrorFilename = "world-error.log";
  368.                 break;
  369.             }
  370.     }
  371.  
  372.     m_normalFile = fopen(logNormalFilename, "a");
  373.     if(m_normalFile == NULL)
  374.         fprintf(stderr, "%s: Error opening '%s': %s\n", __FUNCTION__, logNormalFilename, strerror(errno));
  375.     else
  376.     {
  377.         tm* aTm = localtime(&UNIXTIME);
  378.         outBasic("[%-4d-%02d-%02d %02d:%02d:%02d] ", aTm->tm_year + 1900, aTm->tm_mon + 1, aTm->tm_mday, aTm->tm_hour, aTm->tm_min, aTm->tm_sec);
  379.     }
  380.  
  381.     m_errorFile = fopen(logErrorFilename, "a");
  382.     if(m_errorFile == NULL)
  383.         fprintf(stderr, "%s: Error opening '%s': %s\n", __FUNCTION__, logErrorFilename, strerror(errno));
  384.     else
  385.     {
  386.         tm* aTm = localtime(&UNIXTIME);
  387.         outError("[%-4d-%02d-%02d %02d:%02d:%02d] ", aTm->tm_year + 1900, aTm->tm_mon + 1, aTm->tm_mday, aTm->tm_hour, aTm->tm_min, aTm->tm_sec);
  388.     }
  389. }
  390.  
  391. void oLog::Close()
  392. {
  393.     if(m_normalFile != NULL)
  394.     {
  395.         fflush(m_normalFile);
  396.         fclose(m_normalFile);
  397.         m_normalFile = NULL;
  398.     }
  399.  
  400.     if(m_errorFile != NULL)
  401.     {
  402.         fflush(m_errorFile);
  403.         fclose(m_errorFile);
  404.         m_errorFile = NULL;
  405.     }
  406. }
  407.  
  408. void oLog::SetFileLoggingLevel(int32 level)
  409. {
  410.     //log level -1 is no more allowed
  411.     if(level >= 0)
  412.         m_fileLogLevel = level;
  413. }
  414.  
  415. void SessionLogWriter::write(const char* format, ...)
  416. {
  417.     if(!m_file)
  418.         return;
  419.  
  420.     char out[32768];
  421.     va_list ap;
  422.  
  423.     va_start(ap, format);
  424.     time_t t = time(NULL);
  425.     tm* aTm = localtime(&t);
  426.     sprintf(out, "[%-4d-%02d-%02d %02d:%02d:%02d] ", aTm->tm_year + 1900, aTm->tm_mon + 1, aTm->tm_mday, aTm->tm_hour, aTm->tm_min, aTm->tm_sec);
  427.     size_t l = strlen(out);
  428.     vsnprintf(&out[l], 32768 - l, format, ap);
  429.     fprintf(m_file, "%s\n", out);
  430.     va_end(ap);
  431. }
  432.  
  433. WorldLog::WorldLog()
  434. {
  435.     bEnabled = false;
  436.     m_file = NULL;
  437.  
  438.     if(Config.MainConfig.GetBoolDefault("LogLevel", "World", false))
  439.     {
  440.         Log.Notice("WorldLog", "Enabling packetlog output to \"world.log\"");
  441.         Enable();
  442.     }
  443.     else
  444.     {
  445.         Disable();
  446.     }
  447. }
  448.  
  449. void WorldLog::Enable()
  450. {
  451.     if(bEnabled)
  452.         return;
  453.  
  454.     bEnabled = true;
  455.     if(m_file != NULL)
  456.     {
  457.         Disable();
  458.         bEnabled = true;
  459.     }
  460.     m_file = fopen("world.log", "a");
  461. }
  462.  
  463. void WorldLog::Disable()
  464. {
  465.     if(!bEnabled)
  466.         return;
  467.  
  468.     bEnabled = false;
  469.     if(!m_file)
  470.         return;
  471.  
  472.     fflush(m_file);
  473.     fclose(m_file);
  474.     m_file = NULL;
  475. }
  476.  
  477. WorldLog::~WorldLog()
  478. {
  479.     if(m_file)
  480.     {
  481.         fclose(m_file);
  482.         m_file = NULL;
  483.     }
  484. }
  485.  
  486. void SessionLogWriter::Open()
  487. {
  488.     m_file = fopen(m_filename, "a");
  489. }
  490.  
  491. void SessionLogWriter::Close()
  492. {
  493.     if(!m_file) return;
  494.     fflush(m_file);
  495.     fclose(m_file);
  496.     m_file = NULL;
  497. }
  498.  
  499. SessionLogWriter::SessionLogWriter(const char* filename, bool open)
  500. {
  501.     m_filename = strdup(filename);
  502.     m_file = NULL;
  503.     if(open)
  504.         Open();
  505. }
  506.  
  507. SessionLogWriter::~SessionLogWriter()
  508. {
  509.     if(m_file)
  510.         Close();
  511.  
  512.     free(m_filename);
  513. }
  514.  
Advertisement
Add Comment
Please, Sign In to add comment