好吧,首先到目前为止,这是可行的,但是非常有问题。我希望它能够接受ints
, floats
, doubles
, strings
, 和char*s
. 它通过将所有内容都尝试为 char* 来工作,但如果失败,我希望它作为另一种类型重试。我也想如果我不必传递参数的数量。(更多在底部)
#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;
}
现在,无论如何:
- 让线程输出到控制台,而不会在线程重新加入时使主程序崩溃或挂起(线程池?)
- 使用:(
logHelperMessage(logLevel,firstpram,...)
使用第一个参数获取其内存位置并从那里开始?) - 在:
ss << va_arg(messages,char*);
如果它不能作为一个char*
可能尝试别的东西?
我四处寻找更高级的可变函数,但似乎它们都需要参数数量的参数。或只允许一种类型。另外,如果需要连续循环,我会在程序的其他地方设置一个循环。(我认为这就是一切)