Share Pastebin
Guest
Public paste!

Untitled

By: a guest | Feb 9th, 2010 | Syntax: None | Size: 5.21 KB | Hits: 27 | Expires: Never
Copy text to clipboard
  1. //-----------------------------------------------------------------------------------
  2. //
  3. //   Torque Network Library
  4. //   Copyright (C) 2004 GarageGames.com, Inc.
  5. //   For more information see http://www.opentnl.org
  6. //
  7. //   This program is free software; you can redistribute it and/or modify
  8. //   it under the terms of the GNU General Public License as published by
  9. //   the Free Software Foundation; either version 2 of the License, or
  10. //   (at your option) any later version.
  11. //
  12. //   For use in products that are not compatible with the terms of the GNU
  13. //   General Public License, alternative licensing options are available
  14. //   from GarageGames.com.
  15. //
  16. //   This program is distributed in the hope that it will be useful,
  17. //   but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. //   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19. //   GNU General Public License for more details.
  20. //
  21. //   You should have received a copy of the GNU General Public License
  22. //   along with this program; if not, write to the Free Software
  23. //   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  24. //
  25. //------------------------------------------------------------------------------------
  26.  
  27. #include "tnlLog.h"
  28. #include "tnlDataChunker.h"
  29. #include "tnlPlatform.h"
  30. #include <stdarg.h>
  31. #include <stdio.h>
  32. #include <time.h>
  33. #include <string.h>
  34.  
  35. #ifdef _MSC_VER
  36. #pragma warning (disable: 4996)     // Disable POSIX deprecation, certain security warnings that seem to be specific to VC++
  37. #endif
  38.  
  39. namespace TNL
  40. {
  41.  
  42. LogConsumer *LogConsumer::mLinkedList = NULL;
  43.  
  44. LogConsumer::LogConsumer()    // Constructor
  45. {
  46.    mFilterType = GeneralFilter;
  47.    mNextConsumer = mLinkedList;
  48.    if(mNextConsumer)
  49.       mNextConsumer->mPrevConsumer = this;
  50.    mPrevConsumer = NULL;
  51.    mLinkedList = this;
  52. }
  53.  
  54. LogConsumer::~LogConsumer()
  55. {
  56.    if(mNextConsumer)
  57.       mNextConsumer->mPrevConsumer = mPrevConsumer;
  58.    if(mPrevConsumer)
  59.       mPrevConsumer->mNextConsumer = mNextConsumer;
  60.    else
  61.       mLinkedList = mNextConsumer;
  62. }
  63.  
  64. void LogConsumer::setFilterType(FilterType type)
  65. {
  66.    mFilterType = type;
  67. }
  68.  
  69. LogType *LogType::linkedList = NULL;
  70. LogType *LogType::current = NULL;
  71.  
  72. #ifdef TNL_ENABLE_LOGGING
  73.  
  74. LogType *LogType::find(const char *name)
  75. {
  76.    static ClassChunker<LogType> logTypeChunker(4096);
  77.  
  78.    for(LogType *walk = linkedList; walk; walk = walk->next)
  79.       if(!strcmp(walk->typeName, name))
  80.          return walk;
  81.    LogType *ret = logTypeChunker.alloc();
  82.    ret->next = linkedList;
  83.    linkedList = ret;
  84.    ret->isEnabled = false;
  85.    ret->typeName = name;
  86.    return ret;
  87. }
  88. #endif
  89.  
  90. void LogConsumer::logString(const char *string)
  91. {
  92.    // by default the LogConsumer will output to the platform debug
  93.    // string printer, but only if we're in debug mode
  94.    // this will be overridden by child classes
  95. #ifdef TNL_DEBUG
  96.    Platform::outputDebugString(string);
  97. #endif
  98. }
  99.  
  100. void logger(LogConsumer::FilterType filtertype, const char *format, va_list args)
  101. {
  102.    char buffer[4096];
  103.    U32 bufferStart = 0;
  104.    if(LogType::current)
  105.    {
  106.       strncpy(buffer, LogType::current->typeName, sizeof(buffer));
  107.       bufferStart = strlen(buffer);
  108.  
  109.       buffer[bufferStart] = ':';
  110.       buffer[bufferStart+1] = ' ';
  111.       bufferStart += 2;
  112.    }
  113.  
  114.    // -1 below makes sure we have enough room for a "\n" if we need to append one
  115.    vsnprintf(buffer + bufferStart, sizeof(buffer) - bufferStart - 1, format, args);
  116.    
  117.    // If last char is a "\", chop it off, otherwise append newline
  118.    U32 len = strlen(buffer);  // Should never be >= our buffer length, so appending newline should be ok
  119.  
  120.    if(len > 0 && buffer[len - 1] == '\\')
  121.       buffer[len - 1] = '\0';    // Don't use NULL here, will cause type problems
  122.    else
  123.       strcat(buffer, "\n");
  124.  
  125.    for(LogConsumer *walk = LogConsumer::getLinkedList(); walk; walk = walk->getNext())
  126.       if(walk->mFilterType == filtertype)     // Only log to the requested type of logfile
  127.          walk->logString(buffer);
  128.  
  129.    Platform::outputDebugString(buffer);
  130. }
  131.  
  132.  
  133. void logprintf(const char *format, ...)
  134. {
  135.    va_list s;    
  136.    va_start( s, format );
  137.  
  138.    logger(LogConsumer::GeneralFilter, format, s);
  139.    va_end(s);
  140. }
  141.  
  142. // Log a message to the server log --> wraps logger
  143. void s_logprintf(const char *format, ...)
  144. {
  145.    va_list s;    
  146.    va_start( s, format );
  147.  
  148.    logger(LogConsumer::ServerFilter, format, s);
  149.    va_end(s);
  150. }
  151.  
  152. // Return a nicely formatted date/time stamp
  153. std::string getTimeStamp()
  154. {
  155.   static const U32 TIMESIZE = 40;
  156.   time_t rawtime;
  157.   struct tm * timeinfo;
  158.   char buffer[TIMESIZE];
  159.  
  160.   time ( &rawtime );
  161.   timeinfo = localtime ( &rawtime );
  162.  
  163.   strftime(buffer, TIMESIZE, "%a %d-%m-%Y %H:%M:%S", timeinfo);
  164.  
  165.   return(std::string(buffer));      // Does this not seem a ridiculous use of strings??
  166. }
  167.  
  168.  
  169. // Return a nicely formatted date/time stamp
  170. std::string getShortTimeStamp()
  171. {
  172.   static const U32 TIMESIZE = 40;
  173.   time_t rawtime;
  174.   struct tm * timeinfo;
  175.   char buffer[TIMESIZE];
  176.  
  177.   time ( &rawtime );
  178.   timeinfo = localtime ( &rawtime );
  179.  
  180.   strftime(buffer, TIMESIZE, "%H:%M", timeinfo);
  181.  
  182.   return(std::string(buffer));      // Does this not seem a ridiculous use of strings??
  183. }
  184.  
  185.  
  186. };