I have a custom logging system which allows me to send information to a log file and the console depending on the verbosity currently selected. Right now, the trouble I am having is with the output to the file, with output to the console working fine.
Here is an example:
ilra_talk << "Local IP: " << systemIP() << " | Hostname: " << systemhostname() << endl;
// the systemIP() and systemhostname() functions have already been defined
This should result in the current local IP and hostname of the system being printed to a file. However, it is only resulting in the information being printed to the console, despite how the function is overloaded to result in it printing to both.
I've outlined the code below. Any assistance is appreciated (as always).
A definition currently exists for ilra_talk which results in a new class object being created:
#define ilra_talk ilra(__FUNCTION__,0)
The class definition the following:
class ilra
{
static int ilralevel_set; // properly initialized in my main .cpp
static int ilralevel_passed; // properly initialized in my main .cpp
static bool relay_enabled; // properly initialized in my main .cpp
static bool log_enabled; // properly initialized in my main .cpp
static ofstream logfile; // properly initialized in my main .cpp
public:
// constructor / destructor
ilra(const std::string &funcName, int toset)
{
ilralevel_passed = toset;
}
~ilra(){};
// enable / disable irla functions
static void ilra_verbose_level(int toset){
ilralevel_set = toset;
}
static void ilra_log_enabled(bool toset){
log_enabled = toset;
if (log_enabled == true){
// get current time
time_t rawtime;
time ( &rawtime );
// name of log file (based on time of application start)
stringstream logname_s;
string logname = "rclient-";
logname_s << rawtime;
logname.append(logname_s.str());
// open a log file
logfile.open(logname.c_str());
}
}
// output
template <class T>
ilra &operator<<(const T &v)
{
if(log_enabled == true){ // log_enabled is set to true
logfile << v;
logfile << "Test" << endl; // test will show up, but intended information will not appear
}
if(ilralevel_passed <= ilralevel_set)
std::cout << v;
return *this;
}
ilra &operator<<(std::ostream&(*f)(std::ostream&))
{
if(log_enabled == true) // log_enabled is set to true
logfile << *f;
if(ilralevel_passed <= ilralevel_set)
std::cout << *f;
return *this;
}
}; // end of the class