Page 1 of 1

Logging class

Posted: Sun Oct 26, 2014 2:12 pm
by Seven
I was looking for a small footprint logging class and decided to build my own.
my requirements were
... easy to use
... use of logging level (my app captures all logs and displays in individual windows depending on level)
......my App class overrides the print() function, allowing me to display logs as desired
... able to pass in variable arguments

anyhow, here is a small class for logging that is easily expandable for all irrlicht types
(this example shows only vector3df)

Code: Select all

 
    enum CSLOGTYPE { CSL_INFO, CSL_WARNING, CSL_ERROR, CSL_DEBUG };
    static const char * CSLOGTYPE_STRING[] = { "INFO", "WARNING", "ERROR", "DEBUG" };
 
    class CSLogger
    {
    public:
        CSLogger()          {   }
        virtual ~CSLogger() {   }
    
        virtual void print(CSLOGTYPE type, stringc text) 
        { 
            printf_s("%s - ", CSLOGTYPE_STRING[(int)type]);
            printf_s("%s\n", text.c_str());
        }
 
        int getString(stringc &text, char *szTypes, int index)
        {
            while ((szTypes[index] != '%') && (szTypes[index] != '\0'))
            {
                text += szTypes[index];
                index++;
            }
            return index - 1;
        }
 
        /*
            %i - integer
            %f - float
            %c - char
            %s - char*
            %v - vector3df
                        ...
        */
        void log(CSLOGTYPE type, char *szTypes, ...)
        {
            va_list vl;
            int i;
 
            //  szTypes is the last argument specified; you must access 
            //  all others using the variable-argument macros.
            va_start(vl, szTypes);
 
            stringc text;
 
            // Step through the list.
            for (i = 0; szTypes[i] != '\0'; ++i) 
            {
                switch (szTypes[i]) 
                {   
                    case 'i': text += va_arg(vl, int); break;
                    case 'f': text += va_arg(vl, double); break;
                    case 'c': text += va_arg(vl, char); break;
                    case 's': text += va_arg(vl, char *); break;
                    case 'v': { vector3df vec = va_arg(vl, vector3df); text += vec.X; text += " "; text += vec.Y; text += " "; text += vec.Z; } break;
                    case '%': break;
                    default: i = getString(text,szTypes,i); break;
                }
            }
 
            va_end(vl);
        
            print(type, text);
        }
    };
 
and it is used in this manner

Code: Select all

 
#define CS_LOG Logger.log
 
// output the info to the screen
CS_LOG(CSLOGTYPE::CSL_DEBUG, "Engine Info : \n   WindowSize(%d %d)\n   bits = %d\n   fullscreen = %d\n   stencilbuffer = %d\n   vsync = %d\n", m_WindowSize.Width, m_WindowSize.Height, m_Bits, m_FullScreen, m_UseStencilBuffer, m_VertexShader);
 
 

stored here for future reference.