What is ILog?
ILog is a library that makes logging and assertion quick and easy. ILog is a simple library that is beginner friendly.
Code example:
#include <ILog.h>
int main()
{
I_LOG_ERROR("Something Happened!");
// This will only run in unoptimized builds
I_DEBUG_LOG_ERROR("Another Thing Happened!");
return 0;
}
Console output:
Important Stuff You Should Know About
Note that the color of the output maybe different because of the theme of your terminal and other factors!
Also note that if your compiler doesn't automatically define NDEBUG
when compiling with optomization on, please define this as ILog depeneds on this define to know when you are not compiling in debug mode.
Loging in ILog
ILog's main purpose is to make loging with colours easy and simple. ILog also has loging levels like the error level, the warning level, etc. These do nothing other than make your text a certain colour and have a specific prefix for each level (e.g "Error" for the error level), but it can be useful to quickly figure out if a message is an error or something else. ILog also has a logging levels for debug and optimized modes.
ILog can also log to files streams with a simple command or create a file and write to it.
Code Time!
A Loging function takes in a message then variable arguments.
These variable arguments are useful for added variables values to a message.
You can insert things variables with regular printf formating (e.g. %d
for int's or %p
for pointers).
Example code:
#include <ILog.h>
int main()
{
void* info = malloc(2);
I_LOG_INFO("Hello: %p", info);
I_LOG_TRACE("Hello: %d", 10);
I_LOG_WARNING("Hi: %u", 21);
I_LOG_ERROR(":(%s", ")");
I_LOG_FATAL_ERROR("! %zu", 100);
return 0;
}
Output:
Each ILog macro has a debug equivalent
Example code:
#include <ILog.h>
int main()
{
void* info = malloc(2);
I_DEBUG_LOG_INFO("Hello: %p", info);
I_DEBUG_LOG_TRACE("Hello: %d", 10);
I_DEBUG_LOG_WARNING("Hi: %u", 21);
I_DEBUG_LOG_ERROR(":(%s", ")");
I_DEBUG_LOG_FATAL_ERROR("! %zu", 100);
return 0;
}
Output:
Assertions In ILog
ILog asserts have two levels fatal error and error.
If an assertion fails it will trigger a debug breakpoint (for debug mode) and getchar()
in optomized buids if you are on windows and using the MSVC or Clang + MSVC standard library. If you aren't then it will call the getchar();
function on debug and optomized modes.
An ILog assert takes in two or more arguments a condition, a message and variable arguments (for formating the message). The message will be printed with the file name and the line the assertion took place on if the assertion fails. The assertion works like an if statment if the statment is true then it the assertion fails, if not then it just continuous.
Code example:
#include <ILog.h>
int main()
{
I_ASSERT_ERROR(0, ":)"); // Doesn't fail
I_ASSERT_ERROR(1, "Oh No!");
I_ASSERT_FATAL_ERROR(1, "Oh No! %d", 10);
return 0;
}
Output:
Asserts also have debug equivilants.
Loging to files in ILog
Loging to files is easy and simple with ILog. There is two ways you can log to a file.
-
You can pass in your own
FILE*
into theI_FS_LOG();
macro. TheI_FS_LOG();
macro takes in aFILE*
or an "std stream" like stdout, a message and then variable args (for formating the message). -
You could let ILog handle the file creation and deletion with the
I_FILE_LOG();
macro. TheI_FILE_LOG();
macro takes in a file names, a message, a mode and variable args. Most of the parameters are self explanitory except the mode parameter. The mode parameter corresponds with thefopen();
mode parameter (e.g. "w" for write, "a" for appened, etc.). To learn more about this check out this tutorial by programiz to learn more.
Code example:
#include <ILog.h>
int main()
{
I_FILE_LOG("example.log", "%d World Hello! %d", "w", 5, 6);
FILE* file;
file = fopen("example.log", "a"); // You should error check
I_FS_LOG(file, "Hello World! %d", 10);
return 0;
}
Output:
The file macros do not have debug equivlilents.
What features will ILog get in the future?
I want ILog to have a c++ version that will be more tightly integrated with the c++ way of opening and writing to files and use more c++ features like classes and namespaces.