How to use this by initializing outside main() function so as to make it available to be used in other functions defined in a cpp file.
Jargon4072 opened this issue · 3 comments
I want to initialize it outside the main() function so as I can use it in the other functions present in the file. For example, the initialization process is as below:
const auto logFile = "file_test.log";
L::init(logFile);
L::enableEchoMode(true);
L::setLoggingLevel(L::Level::Trace);
I want to use it as given below:
#include "../../simple_logger.hpp"
// Don't compile asserts away
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
#include <cstdlib>
#include <fstream>
using namespace std;
using juzzlin::L;
const auto logFile = "file_test.log";
L::init(logFile);
L::enableEchoMode(true);
L::setLoggingLevel(L::Level::Trace);
const std::string timestampSeparator = " ## ";
L::setTimestampMode(L::TimestampMode::DateTime, timestampSeparator);
void foo(string name){
L().info() << "Recived name is: "+name+".";
court<<"Hello World "<<name<<endl;
L().info() << "Exiting foo()";
}
int main(){
const std::string message = "Hello, world!";
L().trace() << message;
L().debug() << message;
L().info() << message;
L().warning() << message;
L().error() << message;
L().fatal() << message;
return 0;
}
I am getting errors as shown below:
How to fix this?
The problem here is that your code is not valid C++. You cannot call functions outside the call stack initiating from main():
int someFunction()
{
return 0;
}
someFunction();
^^^^^ This is not legal.
const auto logFile = "file_test.log";
L::init(logFile);
^^^^^ This is not legal.
int main()
{
return 0;
}
Once you have initialized the logger in main()
you can use it from any function so I'm not sure what the actual problem is :) These are global initializations, not per function.
Your code should be something like this:
#include "../../simple_logger.hpp"
// Don't compile asserts away
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
#include <cstdlib>
#include <fstream>
using juzzlin::L;
void foo(string name) {
L().info() << "Recived name is: " << name << ".";
std::cout << "Hello World " << name << std::endl;
L().info() << "Exiting foo()";
}
void bar(string name) {
L().info() << "Recived name is: " << name << ".";
std::cout << "Hello World " << name << std::endl;
L().info() << "Exiting bar()";
}
int main() {
// Just init the logger once here. This will always be called before foo() and bar().
const auto logFile = "file_test.log";
L::init(logFile);
L::enableEchoMode(true);
L::setLoggingLevel(L::Level::Trace);
const std::string timestampSeparator = " ## ";
L::setTimestampMode(L::TimestampMode::DateTime, timestampSeparator);
const std::string message = "Hello, world!";
L().trace() << message;
L().debug() << message;
L().info() << message;
L().warning() << message;
L().error() << message;
L().fatal() << message;
return 0;
}
Thanks for pointing that out. I was actually looking to use this in a library coded in c++ which doesn't have main()
in it. I ended up coding a basic logger by myself :).
Note that the initialization doesn't have to be exactly in main. It can be in any function that's being called first, like some library initialization function 😊