I'm writting some logic to do some logging, there's a bunch of C++ mixed with C as most of this library is meant to be p/Invoked. I've managed to write a function that logs a message along with an optional parameter:
void writeToLog(char* message, char* arg) {
std::ofstream file;
file.open(fullpath, std::ios::in | std::ios::app);
if (file.is_open()) {
std::string fullMessage = getCurrentDateTime();
fullMessage.append(message);
if (arg != 0)
fullMessage.append(arg);
fullMessage.append("\n");
const char* pcMessage = fullMessage.c_str();
file << pcMessage;
std::cout << pcMessage;
}
file.close();
}
However it only takes char* as args, but I'd like to use them with int and long as well... I have tried:
void writeToLog(char* message, void* arg) {
std::ofstream file;
file.open(fullpath, std::ios::in | std::ios::app);
if (file.is_open()) {
std::string fullMessage = getCurrentDateTime();
fullMessage.append(message);
if (arg != 0)
fullMessage.append((char*)&arg);
fullMessage.append("\n");
const char* pcMessage = fullMessage.c_str();
file << pcMessage;
std::cout << pcMessage;
}
file.close();
}
But it prints/writes gibberish, regardless of data type. Please point to any other errors as you see fit, I'm a bit of a noob when it comes to C/C++.