From cad9b49ffd79d98005612f81c97e0bb0fcb673ba Mon Sep 17 00:00:00 2001 From: "Martin Felis (berta)" Date: Wed, 14 Apr 2010 20:13:19 +0200 Subject: [PATCH] added string conversion for enums EngineStatus and LogLevel --- engine/CMakeLists.txt | 1 + engine/Engine.cc | 3 ++- engine/Engine.h | 11 +---------- engine/EngineEnums.h | 39 +++++++++++++++++++++++++++++++++++++++ engine/EnumStrings.cc | 11 +++++++++++ engine/EnumStrings.h | 15 +++++++++++++++ engine/EnumToString.h | 21 ++++++++++++++++----- engine/Logging.cc | 7 ++++++- engine/Logging.h | 1 + engine/LoggingGlobal.h | 15 +++++---------- 10 files changed, 97 insertions(+), 27 deletions(-) create mode 100644 engine/EngineEnums.h create mode 100644 engine/EnumStrings.cc create mode 100644 engine/EnumStrings.h diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index c7a3530..bee5431 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -31,6 +31,7 @@ SET ( ENGINE_SRCS Engine.cc Logging.cc + EnumStrings.cc ) INCLUDE_DIRECTORIES ( diff --git a/engine/Engine.cc b/engine/Engine.cc index 4a155db..a061fc3 100644 --- a/engine/Engine.cc +++ b/engine/Engine.cc @@ -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; } diff --git a/engine/Engine.h b/engine/Engine.h index 0be3c54..acb0c67 100644 --- a/engine/Engine.h +++ b/engine/Engine.h @@ -15,6 +15,7 @@ #include #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 diff --git a/engine/EngineEnums.h b/engine/EngineEnums.h new file mode 100644 index 0000000..e268884 --- /dev/null +++ b/engine/EngineEnums.h @@ -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 */ diff --git a/engine/EnumStrings.cc b/engine/EnumStrings.cc new file mode 100644 index 0000000..98f2f97 --- /dev/null +++ b/engine/EnumStrings.cc @@ -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 diff --git a/engine/EnumStrings.h b/engine/EnumStrings.h new file mode 100644 index 0000000..40013f2 --- /dev/null +++ b/engine/EnumStrings.h @@ -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 */ diff --git a/engine/EnumToString.h b/engine/EnumToString.h index 9e728ca..9b9c09a 100644 --- a/engine/EnumToString.h +++ b/engine/EnumToString.h @@ -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: + * http://www.codeproject.com/KB/cpp/C___enums_to_strings.aspx + * + * 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 + * const char* GetStringMyEnum(MyEnum tagMyEnum);. + * + */ #undef DECL_ENUM_ELEMENT #undef BEGIN_ENUM diff --git a/engine/Logging.cc b/engine/Logging.cc index 59f1842..a008579 100644 --- a/engine/Logging.cc +++ b/engine/Logging.cc @@ -5,6 +5,9 @@ #include #include #include +#include + +// #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(); } } diff --git a/engine/Logging.h b/engine/Logging.h index 7804dc7..b519027 100644 --- a/engine/Logging.h +++ b/engine/Logging.h @@ -4,6 +4,7 @@ #include "Engine.h" namespace Engine { + class Module; /** \brief All logging goes through this class diff --git a/engine/LoggingGlobal.h b/engine/LoggingGlobal.h index 468eb8f..75c2d3a 100644 --- a/engine/LoggingGlobal.h +++ b/engine/LoggingGlobal.h @@ -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 */