Changeset 874


Ignore:
Timestamp:
05/05/19 06:21:06 (5 months ago)
Author:
Maciej Komosinski
Message:

Introduced a function logLevelName() to avoid potential errors, e.g. forgetting "+1" in LOG_LEVEL[level+1]

Location:
cpp/common
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/log.cpp

    r841 r874  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    88#include "Convert.h"
    99
    10 const char* LOG_LEVEL[] = { "[DEBUG] ", "", "[WARN] ", "[ERROR] ", "[CRITICAL] " };
     10const char* LOG_LEVEL_ARRAY[] = { "[DEBUG] ", "", "[WARN] ", "[ERROR] ", "[CRITICAL] " };
    1111
    1212void logPrintf_va(const char *obj, const char *method, int level, const char *msgf, va_list va)
     
    3131        va_end(argptr);
    3232}
     33
     34const char* logLevelName(int level)
     35{
     36        level = min(LOG_CRITICAL, max(LOG_DEBUG, level));
     37        return LOG_LEVEL_ARRAY[level + 1];
     38}
  • cpp/common/log.h

    r511 r874  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    88#include <stdarg.h>
    99
    10 extern const char* LOG_LEVEL[];
     10extern const char* LOG_LEVEL_ARRAY[];
    1111#define LOG_FORMAT "%s%s.%s: %s"
    1212#define LOG_MULTILINE_CONTINUATION "..."
    13 
    1413
    1514void logPrintf(const char *obj, const char *method, int level, const char *msgf, ...);
     
    1716void log_printf(const char *msgf, ...); //a shorthand for printf (a different name again to avoid the risk of confusion with the two functions above. This would be unlikely but possible when the argument types would match)
    1817void logMessage(const char *obj, const char *method, int level, const char *msg);
     18const char* logLevelName(int level);
    1919
    2020
  • cpp/common/loggers/loggers.cpp

    r873 r874  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    148148                        {
    149149                                if (msgs.length() > 0) msgs += '\n';
    150                                 msgs += ssprintf(LOG_FORMAT, LOG_LEVEL[level + 1], obj, method, msg);
     150                                msgs += ssprintf(LOG_FORMAT, logLevelName(level), obj, method, msg);
    151151                        }
    152152                }
     
    170170        return string("");
    171171}
    172 
  • cpp/common/loggers/loggertostdout.cpp

    r522 r874  
    11// This file is a part of Framsticks SDK.  http://www.framsticks.com/
    2 // Copyright (C) 1999-2015  Maciej Komosinski and Szymon Ulatowski.
     2// Copyright (C) 1999-2019  Maciej Komosinski and Szymon Ulatowski.
    33// See LICENSE.txt for details.
    44
     
    1515
    1616const char** LoggerToStdout::default_log_level[] =
    17 { LOG_LEVEL, default_log_level_ansicolor };
     17{ LOG_LEVEL_ARRAY, default_log_level_ansicolor };
    1818
    1919const char* LoggerToStdout::default_log_format[] = //note trailing %s (so it's easy to append "\n" later)
     
    2929void LoggerToStdout::handleSingleLine(const char *obj, const char *method, int level, const char *msg)
    3030{
    31         if (level < -1) level = -1; else if (level>3) level = 3;
    3231#ifdef SHP
    33         AppLog(LOG_FORMAT "\n",LOG_LEVEL[level+1],obj,method,msg);
     32        AppLog(LOG_FORMAT "\n", logLevelName(level), obj, method, msg);
    3433#else
    3534        if (file)
    36                 file->printf(LOG_FORMAT "\n", LOG_LEVEL[level + 1], obj, method, msg);
     35                file->printf(LOG_FORMAT "\n", logLevelName(level), obj, method, msg);
    3736        else
     37        {
     38                if (level < -1) level = -1; else if (level > 3) level = 3;
    3839                printf(log_format, log_level[level + 1], obj, method, msg, "\n");
     40        }
    3941#endif
    4042}
Note: See TracChangeset for help on using the changeset viewer.