Alright, first of all so far this works, but is extremely buggy. I want it to be able to take ints
, floats
, doubles
, strings
, and char*s
. It sort of works by trying everything as a char* but if that fails i would like it retry it as another type. I would also like if i didn't have to pass in the number of params. (more at bottom)
#include <iostream>
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <windows.h>
#include <ctime>
#include <tchar.h>
#include <stdio.h>
#include <vector>
#include <thread>
const enum loglevel{INFO,WARNING,OK,SEVERE};
void logHelperMessage(loglevel,int, ...);
void threadedloghelpermessage(loglevel,string);
int main(int argc, char **argv)
{
logHelperMessage(INFO,4,"Hi","I","DO","WORK");
}
void logHelperMessage(loglevel severity,int number, ...)
{
va_list messages;
va_start(messages,number);
std::stringstream ss;
for(int i = 0;i < number;i++)
{
ss << va_arg(messages,char*);
}
std::string s = ss.str();
thread t1(threadedloghelpermessage,severity,s);
t1.join();
}
void threadedloghelpermessage(loglevel severity,string message)
{
//TODO: implement a stack?
switch (severity)
{
case INFO:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_BLUE);
cout << "[IF]";
break;
case WARNING:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x06);
cout << "[WA]";
break;
case OK:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
cout << "[OK]";
break;
case SEVERE:
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_RED);
cout << "[ER]";
break;
default:
break;
}
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x08);
time_t t = time(0);
struct tm now;
localtime_s(&now, &t );
cout << "[";
int hour = now.tm_hour;
if(hour < 10)
{
cout << 0 << hour << ":";
}
else
{
cout << hour << ":";
}
int minu = now.tm_min;
if(minu < 10)
{
cout << 0 << minu << ":";
}
else
{
cout << minu << ":";
}
int sec = now.tm_sec;
if(sec < 10)
{
cout << 0 << sec;
}
else
{
cout << sec;
}
cout << "] ";
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),0x07);
cout << message << endl;
}
Now,is there anyway to:
- Have the threads output to the console without them crashing or hanging the main program as the thread rejoins (threadpool?)
- Use:
logHelperMessage(logLevel,firstpram,...)
(use the first param to get its mem position and go from there?) - In the:
ss << va_arg(messages,char*);
if it fails to work as achar*
possibly try something else?
I looked around for more advanced varidic functions, but it seems all of them require a pramiter of the number of arguments. or only allowing one type. Also, if a continuous loop is needed, I have a loop set up somewhere else in the program. (I think that's everything)