C++ 简单的打印日志代码片段
C++ 简单的打印日志代码片段
int print_log1(const wchar_t* text) { if(0 == debug_log) { return 0; } std::string str = LPCTSTR2string(text); return print_log2(str); } int print_log2(const std::string text) { if(0 == debug_log) { return 0; } int len = text.length() ; char* cstr = new char[128]; int i; for( i=0;i<text.length();i++) cstr[i] = text[i]; cstr[i] = '\0'; char end[2] = {'\r','\0'}; //首先判断文件是否存在,如果不存在则创建,并在开头加入0xfeff;如果存在则直接写入 FILE* fp; fopen_s(&fp, logFilePath2, "at"); if (fp != NULL) { SYSTEMTIME st; GetLocalTime(&st); char buf[128] = { 0 }; sprintf_s(buf, 128, "%04d%02d%02d %02d:%02d:%02d:%03d ", st.wYear, st.wMonth, st.wDay, st.wHour, st.wMinute, st.wSecond, st.wMilliseconds); fwrite(buf, strlen(buf), 1, fp); //fwrite(text, sizeof(wchar_t), wcslen(text), fp); //fwrite(text, strlen(text), 1, fp); fwrite(cstr, i+1, 1, fp); fwrite(end, strlen(end), 1, fp); //fflush(fp); fclose(fp); } delete[]cstr; cstr=NULL; return 1; }