Using software, the log can provide more running information, and studying the log is also a kind of learning.
Godot has two modes of operation: with console and without console. In the case of a console, a DOS window will be opened when running, and the specific log information will be displayed in the DOS window. But without a console, as far as I know, these logs don't know where to display them.
DrGraph prefix output
Based on single-step debugging, it is found that various log outputs will call the logv and log_error functions of each logger of CompositeLogger, and when running under windows, there will be a WindowsTerminalLogger instance object, which will eventually call the WindowsTerminalLogger::logv/log_error function. In order to see if it works, I added the DrGraph prefix to the output in WindowsTerminalLogger::logv.
void WindowsTerminalLogger::logv(const char *p_format, va_list p_list, bool p_err) {
if (!should_log(p_err)) {
return;
}
const unsigned int BUFFER_SIZE = 16384;
char buf[BUFFER_SIZE + 1]; // +1 for the terminating character
int len = vsnprintf(buf, BUFFER_SIZE, p_format, p_list);
if (len <= 0) {
return;
}
if ((unsigned int)len >= BUFFER_SIZE) {
len = BUFFER_SIZE; // Output is too big, will be truncated
}
buf[len] = 0;
int wlen = MultiByteToWideChar(CP_UTF8, 0, buf, len, nullptr, 0);
if (wlen < 0) {
return;
}
wchar_t *wbuf = (wchar_t *)memalloc((len + 1) * sizeof(wchar_t));
ERR_FAIL_NULL_MSG(wbuf, "Out of memory.");
MultiByteToWideChar(CP_UTF8, 0, buf, len, wbuf, wlen);
wbuf[wlen] = 0;
if (p_err) {
fwprintf(stderr, L"DrGraph - %ls", wbuf);
} else {
wprintf(L"DrGraph - %ls", wbuf);
}
memfree(wbuf);
#ifdef DEBUG_ENABLED
fflush(stdout);
#endif
}
It seems that log_error will eventually call logv, so there is no need to change the rest. After compiling, run it with the console mode, and it really changed to the place:
log file output
For non-console mode startup and operation, the log output function should have no place to display output.
if (p_err) {
fwprintf(stderr, L"DrGraph - %ls", wbuf);
} else {
wprintf(L"DrGraph - %ls", wbuf);
}
A log output file can be added to record this information. Simply implement some functional functions through ofstream:
std::string logFileName = "";
std::string getLogFileName() {
if (logFileName.length() == 0) {
TCHAR szFileName[256];
memset(szFileName, 0, sizeof(szFileName));
GetModuleFileName(NULL, szFileName, sizeof(szFileName));
logFileName = szFileName;
int nPos = logFileName.rfind("\\");
if (nPos != -1)
logFileName.erase(nPos, logFileName.size() - nPos);
logFileName += "\\DrGraph_debug.log";
}
return logFileName;
}
std::ofstream * getLogStream() {
std::ios_base::openmode mode = logFileName.length() > 0 ? std::ios_base::app : std::ios_base::trunc;
std::string fileName = getLogFileName();
std::ofstream * os = new std::ofstream(fileName, mode);
if (!os->is_open())
fwprintf(stderr, L"DrGraph - Can't open log file %hs to write", fileName.c_str()); // %Ts
return os;
}
std::string dateTimePrefix() {
struct tm *ptr;
time_t lt;
char str[80];
lt = time(NULL);
ptr = localtime(<);
strftime(str, 100, "%F %T", ptr);
auto now = std::chrono::system_clock::now();
std::chrono::milliseconds ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
std::ostringstream oss;
oss.fill('0');
oss << str << ":" << std::setw(3) << ms.count() << " > Log by DrGraph > ";
return oss.str();
}
Add custom log output after logv call
std::ofstream *os = getLogStream();
(*os) << dateTimePrefix() << buf;
delete os;
In this way, after running, the log file DrGraph_debug.log will be automatically generated
plus a macro definition
#define DEBUG_SOURCE_POS print_line("[SourceCode Position Info] > Function", __FUNCTION__, "@File", __FILE__, "::Line", __LINE__)
Add DEBUG_SOURCE_POS to the place where the log is to be output, and you can see the simple source code information
Feel much better.