Ignore:
Timestamp:
05/23/16 13:48:45 (8 years ago)
Author:
Maciej Komosinski
Message:

Improved handling of multiline messages by loggers

File:
1 edited

Legend:

Unmodified
Added
Removed
  • cpp/common/loggers/loggers.cpp

    r452 r511  
    55#include "loggers.h"
    66#include <common/stl-util.h>
     7#include <string.h>
    78
    8 void _logMessageSingleLine(const char *obj, const char *method, int level, const char *msg)
     9void logMessage(const char *obj, const char *method, int level, const char *msg)
    910{
    1011        tlsGetRef(message_handler_manager_instance).send(obj, method, level, msg);
     
    101102}
    102103
     104void LoggerBase::handle(const char *obj, const char *method, int level, const char *msg)
     105{
     106        int line = 0; //all lines except the first one get the "..." prefix
     107        const char* nextsep;
     108        do
     109        {
     110                nextsep = strchr(msg, '\n');
     111                if (nextsep == NULL) //last chunk, until the end
     112                        nextsep = strchr(msg, '\0');
     113                if ((nextsep > msg) && (nextsep[-1] == '\r'))
     114                        nextsep--;
     115                if (line == 0)
     116                {
     117                        if (*nextsep == 0) //there was only one line! no need to modify it in any way.
     118                                handleSingleLine(obj, method, level, msg);
     119                        else //first line from multi-line
     120                                handleSingleLine(obj, method, level, string(msg, nextsep - msg).c_str());
     121                }
     122                else //consecutive lines from multi-line
     123                        handleSingleLine(obj, method, level, (LOG_MULTILINE_CONTINUATION + string(msg, nextsep - msg)).c_str()); //could also add line numbers like ...(3)... but let's keep the prefix short and simple
     124                line++;
     125                if ((nextsep[0] == '\r') && (nextsep[1] == '\n'))
     126                        msg = nextsep + 2;
     127                else if (*nextsep)
     128                        msg = nextsep + 1;
     129        } while (*nextsep);
     130}
     131
    103132/////////////////////////////////
    104133
Note: See TracChangeset for help on using the changeset viewer.