added string conversion for enums EngineStatus and LogLevel

main
Martin Felis (berta) 2010-04-14 20:13:19 +02:00
parent a5ee2fe1f5
commit cad9b49ffd
10 changed files with 97 additions and 27 deletions

View File

@ -31,6 +31,7 @@ SET ( ENGINE_SRCS
Engine.cc
Logging.cc
EnumStrings.cc
)
INCLUDE_DIRECTORIES (

View File

@ -226,7 +226,8 @@ void Engine::OnDestroy () {
* Module specific functions
*/
void Engine::SetStatus (EngineStatus new_status) {
LogDebug ("EngineStatus Change: '%d' -> '%d'", mStatus, new_status);
LogDebug ("EngineStatus Change: '%s' -> '%s'", GetStringEngineStatus(mStatus), GetStringEngineStatus(new_status));
// LogDebug ("EngineStatus Change: '%d' -> '%d'", mStatus, new_status);
mStatus = new_status;
}

View File

@ -15,6 +15,7 @@
#include <bitset>
#include "Module.h"
#include "EngineEnums.h"
// Some ugly #defines
@ -41,16 +42,6 @@ class Commands;
class Variables;
class Variable;
enum EngineStatus {
EngineStatusUndefined = 0,
EngineStatusInitializing,
EngineStatusInitialized,
EngineStatusRunning,
EngineStatusStopping,
EngineStatusStopped,
EngineStatusDestroying
};
/** \brief The outermost class which contains just everything!
*
* Engine::Engine takes care of initializing, running and destroying the whole

39
engine/EngineEnums.h Normal file
View File

@ -0,0 +1,39 @@
/** \brief This file contains all the enums of the engine that will provide enum to string conversion
*
* See file \ref EnumToString.h for details.
*/
#if ( !defined(_ENGINEENUMS_H) || defined(GENERATE_ENUM_STRINGS) )
#if ( !defined(GENERATE_ENUM_STRINGS))
#define _ENGINEENUMS_H
#endif
#include "EnumToString.h"
namespace Engine {
BEGIN_ENUM(EngineStatus)
{
DECL_ENUM_ELEMENT(EngineStatusUndefined),
DECL_ENUM_ELEMENT(EngineStatusInitializing),
DECL_ENUM_ELEMENT(EngineStatusInitialized),
DECL_ENUM_ELEMENT(EngineStatusRunning),
DECL_ENUM_ELEMENT(EngineStatusStopping),
DECL_ENUM_ELEMENT(EngineStatusStopped),
DECL_ENUM_ELEMENT(EngineStatusDestroying)
}
END_ENUM(EngineStatus)
BEGIN_ENUM(LogLevel)
{
DECL_ENUM_ELEMENT(LogLevelDebug),
DECL_ENUM_ELEMENT(LogLevelWarning),
DECL_ENUM_ELEMENT(LogLevelMessage),
DECL_ENUM_ELEMENT(LogLevelError)
}
END_ENUM(LogLevel)
}
#endif /* _ENGINEENUMS_H */

11
engine/EnumStrings.cc Normal file
View File

@ -0,0 +1,11 @@
/** \brief This file contains after preprocessing the code for enum to string conversion.
*
* See file \ref EnumToString.h for details.
*/
#include "EnumStrings.h"
#define GENERATE_ENUM_STRINGS
#include "EnumStrings.h"
#undef GENERATE_ENUM_STRINGS

15
engine/EnumStrings.h Normal file
View File

@ -0,0 +1,15 @@
/** \brief This file contains all includes which define enums that provide string conversion
*
* See file \ref EnumToString.h for details.
*/
#if ( !defined(_ENUMSTRINGS_H) || defined(GENERATE_ENUM_STRINGS) )
#if ( !defined(GENERATE_ENUM_STRINGS))
#define _ENUMSTRINGS_H
#endif
#include "EnumToString.h"
#include "EngineEnums.h"
#endif /* _ENUMSTRINGS_H */

View File

@ -1,8 +1,19 @@
// File name: "EnumToString.h"
//
// From: http://www.codeproject.com/KB/cpp/C___enums_to_strings.aspx
//
// Thanks to Marcos F. Cardoso
/** \brief enum to string conversion
*
* This is the heart of the 'automatic' enum to string conversion and taken
* from:
* <a href="http://www.codeproject.com/KB/cpp/C___enums_to_strings.aspx">http://www.codeproject.com/KB/cpp/C___enums_to_strings.aspx</a>
*
* Thanks to Marcos F. Cardoso.
*
* With the preprocessor #defines from this file and a special inclusion and
* enum definition strategy the code for enum to string conversion is
* automatically created.
*
* For an enum of name "MyEnum" the conversion function will be called
* <code>const char* GetStringMyEnum(MyEnum tagMyEnum);</code>.
*
*/
#undef DECL_ENUM_ELEMENT
#undef BEGIN_ENUM

View File

@ -5,6 +5,9 @@
#include <cstdlib>
#include <assert.h>
#include <ctime>
#include <fstream>
// #include "EnumToString.h"
namespace Engine {
@ -66,7 +69,9 @@ void Logging::Log (LogLevel level, const char *str, ...) {
/// \TODO also write out the log level
time_t timer = time(NULL);
std::string timestr (asctime(localtime(&timer)));
mLogFileOut << timestr.substr(0, timestr.size() - 1) << ' ' << msg << std::endl;
mLogFileOut << timestr.substr(0, timestr.size() - 1) << ' '
<< GetStringLogLevel(level)
<< ": " << msg << std::endl;
mLogFileOut.flush();
}
}

View File

@ -4,6 +4,7 @@
#include "Engine.h"
namespace Engine {
class Module;
/** \brief All logging goes through this class

View File

@ -1,15 +1,10 @@
#ifndef _LOGGINGLOBAL_H
#define _LOGGINGLOBAL_H
#ifndef _LOGGINGGLOBAL_H
#define _LOGGINGGLOBAL_H
#include "EngineEnums.h"
namespace Engine {
enum LogLevel {
LogLevelDebug = 0,
LogLevelWarning,
LogLevelMessage,
LogLevelError
};
/** \brief Represents a log message along with its level
*/
struct LogEntry {
@ -44,4 +39,4 @@ const LogEntry &GetLastLogEntry ();
}
#endif // _LOGGINGLOBAL_H
#endif /* _LOGGINGGLOBAL_H */