When this is in the main.cpp it works, but if i put in log.cpp gets LNK2019 error when i call log function with any number of arguments.
string getDate()
{
SYSTEMTIME localTime;
TCHAR strTime[128];
GetLocalTime(&localTime);
wsprintf(strTime, "%04d_%02d_%02d_", localTime.wYear, localTime.wMonth, localTime.wDay);
stringstream strm;
strm << strTime;
string date = strm.str();
return &date[0];
}
string getHour()
{
SYSTEMTIME localTime;
TCHAR strTime[128];
GetLocalTime(&localTime);
wsprintf(strTime, "%02d:%02d:%02d >> ", localTime.wHour, localTime.wMinute, localTime.wSecond);
stringstream strm;
strm << strTime;
string hour = strm.str();
return &hour[0];
}
void addToStream(stringstream& /*a_stream*/)
{
}
template<typename T, typename... Args>
void addToStream(stringstream& a_stream, const T& a_value, Args... a_args)
{
a_stream << a_value;
addToStream(a_stream, a_args...);
}
template<typename... Args>
void log(Args... a_args)
{
stringstream strm;
addToStream(strm, a_args...);
string s = strm.str();
HKEY key;
char appPath[1024];
DWORD appPathLength = 1024;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, "System\\CurrentControlSet\\Control\\Session Manager\\Environment", 0, KEY_READ | KEY_WRITE, &key) != ERROR_SUCCESS)
{
RegCloseKey(key);
}
else
{
if (RegQueryValueEx(key, "SERVIDOR_DIR", NULL, NULL, (LPBYTE)&appPath, &appPathLength) != ERROR_FILE_NOT_FOUND)
{
HANDLE hFile;
string dateProv = getDate();
char *date = new char[dateProv.size() + 1];
copy(dateProv.begin(), dateProv.end(), date);
date[dateProv.size()] = '\0';
char fileNamePath[MAX_PATH];
strcpy(fileNamePath, appPath);
strcat(fileNamePath, "\\logs\\painel\\");
strcat(fileNamePath, date);
strcat(fileNamePath, "levaetraz.log");
puts(fileNamePath);
hFile = CreateFile(
fileNamePath, // file to open
GENERIC_WRITE, // open for writing
0, // share for writing
NULL, // default security
// CREATE_NEW, // existing file only
//CREATE_ALWAYS, // creates a new file, always.
OPEN_ALWAYS, // creates a new file, always.
FILE_ATTRIBUTE_NORMAL, // normal file
NULL // no attr. template
);
if (hFile != INVALID_HANDLE_VALUE)
{
// Write to File
BOOL bErrorFlag = FALSE;
string hourProv = getHour();
char *hour = new char[hourProv.size() + 1];
copy(hourProv.begin(), hourProv.end(), hour);
hour[hourProv.size()] = '\0';
char stringLog[1024];
strcpy(stringLog, hour);
strcat(stringLog, &s[0]);
strcat(stringLog, "\r\n");
puts(stringLog);
DWORD dwPtr = SetFilePointer(hFile, 0, NULL, FILE_END); //set pointer position to end file
DWORD dwBytesToWrite = lstrlen(stringLog);
DWORD a = 0;
bErrorFlag = WriteFile(
hFile, // open file handle
stringLog, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwPtr, // number of bytes that were written
NULL // no overlapped structure
);
delete[] hour;
}
delete[] date;
CloseHandle(hFile);
RegCloseKey(key);
}
RegCloseKey(key);
}
}
Before i put the possibility to variable number of arguments and call log("any thing") works, so how this gonna work in log.cpp with variable number of arguments?
void log(char *log)
{
HANDLE hFile;
std::string dateProv = getDate();
char *date = new char[dateProv.size() + 1];
std::copy(dateProv.begin(), dateProv.end(), date);
date[dateProv.size()] = '\0';
char fileNamePath[MAX_PATH];
strcpy(fileNamePath, "logs/painel/");
strcat(fileNamePath, date);
strcat(fileNamePath, "levaetraz.log");
puts(fileNamePath);
hFile = CreateFile(
fileNamePath, // file to open
GENERIC_WRITE, // open for writing
0, // share for writing
NULL, // default security
// CREATE_NEW, // existing file only
//CREATE_ALWAYS, // creates a new file, always.
OPEN_ALWAYS, // creates a new file, always.
FILE_ATTRIBUTE_NORMAL, // normal file
NULL // no attr. template
);
if (hFile != INVALID_HANDLE_VALUE)
{
// Write to File
BOOL bErrorFlag = FALSE;
std::string hourProv = getHour();
char *hour = new char[hourProv.size() + 1];
std::copy(hourProv.begin(), hourProv.end(), hour);
hour[hourProv.size()] = '\0';
char stringLog[1024];
strcpy(stringLog, hour);
strcat(stringLog, log);
strcat(stringLog, "\r\n");
puts(stringLog);
DWORD dwPtr = SetFilePointer(hFile, 0, NULL, FILE_END); //set pointer position to end file
DWORD dwBytesToWrite = lstrlen(stringLog);
DWORD a = 0;
bErrorFlag = WriteFile(
hFile, // open file handle
stringLog, // start of data to write
dwBytesToWrite, // number of bytes to write
&dwPtr, // number of bytes that were written
NULL // no overlapped structure
);
delete[] hour;
}
delete[] date;
CloseHandle(hFile);
}
I've created a log.h and include in main.cpp
#pragma once
template<typename... Args>
void log(Args... a_args);
What is missing?