Add function to read whole of a (maybe active) logfile into memory

This commit is contained in:
baldurk
2017-12-18 17:05:38 +00:00
parent fd09c323b3
commit a104d8516e
3 changed files with 50 additions and 0 deletions
+4
View File
@@ -290,6 +290,10 @@ bool logfile_open(const char *filename);
void logfile_append(const char *msg, size_t length);
void logfile_close(const char *filename);
// read the whole logfile into memory. This may race with processes writing, but it will read the
// whole of the file at some point. Useful since normal file reading may fail on the shared logfile
std::string logfile_readall(const char *filename);
// utility functions
inline bool dump(const char *filename, const void *buffer, size_t size)
{
+22
View File
@@ -484,6 +484,28 @@ void ReleaseFDAfterFork()
close(logfileFD);
}
std::string logfile_readall(const char *filename)
{
FILE *f = FileIO::fopen(filename, "r");
std::string ret;
if(f == NULL)
return ret;
FileIO::fseek64(f, 0, SEEK_END);
uint64_t size = FileIO::ftell64(f);
FileIO::fseek64(f, 0, SEEK_SET);
ret.resize((size_t)size);
FileIO::fread(&ret[0], 1, ret.size(), f);
FileIO::fclose(f);
return ret;
}
bool logfile_open(const char *filename)
{
logfileFD = open(filename, O_APPEND | O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
+24
View File
@@ -636,6 +636,30 @@ bool logfile_open(const char *filename)
return logHandle != NULL;
}
std::string logfile_readall(const char *filename)
{
wstring wfn = StringFormat::UTF82Wide(string(filename));
HANDLE h = CreateFileW(wfn.c_str(), FILE_READ_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
std::string ret;
if(h != NULL)
{
DWORD len = GetFileSize(h, NULL);
ret.resize(len);
DWORD dummy = len;
ReadFile(h, &ret[0], len, &dummy, NULL);
CloseHandle(h);
}
return ret;
}
void logfile_append(const char *msg, size_t length)
{
if(logHandle)