Harmonic Flow Framework (libhffwk)
Cross platform C++ 2D Game Engine Framework
Log.cpp
1 /*
2  Harmonic Flow Framework
3  Copyright (C) 2018-2018 Andreas Widen <andreas@harmonicflow.org>
4 
5  This software is provided 'as-is', without any express or implied
6  warranty. In no event will the authors be held liable for any damages
7  arising from the use of this software.
8 
9  Permission is granted to anyone to use this software for any purpose,
10  including commercial applications, and to alter it and redistribute it
11  freely, subject to the following restrictions:
12 
13  1. The origin of this software must not be misrepresented; you must not
14  claim that you wrote the original software. If you use this software
15  in a product, an acknowledgment in the product documentation would be
16  appreciated but is not required.
17  2. Altered source versions must be plainly marked as such, and must not be
18  misrepresented as being the original software.
19  3. This notice may not be removed or altered from any source distribution.
20 */
21 #include "Log.h"
22 
23 #include "platform/FileDirUtils.h"
24 #include "platform/PakInterface.h"
25 #include <string>
26 
27 #include <assert.h>
28 
29 using namespace HFCore;
30 
31 #include "debug/CrtDbgNew.h"
32 
33 // global for log file:
34 HFCore::PFILE *gLogFile = NULL;
35 
36 void Log::init(FileDirUtils *fdu, const UString &publisherName,
37  const UString &windowTitle)
38 {
39  HFCore::UString unicodeModeStr = L"w+b";
40  HFCore::UString modeStr = "w+b";
41 
42  std::wstring basePath = fdu->getAppDataFolder();
43  HFCore::UString logFileName = windowTitle;
44  logFileName.append(".log");
45  std::wstring pathToCreateLog = L"";
46 
47  // Create publisher dir:
48  std::wstring pathToCreate = L"";
49 #if defined(HF_PLATFORM_LINUX)
50  pathToCreate = basePath + L"/." + publisherName.wc_str() + L"/";
51 #else
52  pathToCreate = basePath + L"/" + publisherName.wc_str() + L"/";
53 #endif
54  fdu->createDirectory(pathToCreate);
55 
56  // Create publisher/windowtitle dir:
57  pathToCreate = pathToCreate + windowTitle.wc_str() + L"/";
58  fdu->createDirectory(pathToCreate);
59 
60  // create log file:
61  pathToCreateLog = pathToCreate + logFileName.wc_str();
62  gLogFile = p_fopen(pathToCreateLog.c_str(), unicodeModeStr.wc_str());
63  assert(gLogFile != NULL);
64 
65  log("=====================================================\r\n");
66  log("= %s Log\r\n", windowTitle.toUtf8());
67  log("=====================================================\r\n");
68 
69  std::string appFolder = HFCore::StringUtil::WStringToString(pathToCreate);
70  log("Application Data Folder: %s\r\n", appFolder.c_str());
71 }
72 
74 {
75  log("log closed.\r\n");
76  p_fclose(gLogFile);
77 }
78 
79 void Log::log(const int8_hf *fmt, ...)
80 {
81  if (gLogFile != NULL)
82  {
83  va_list ap2;
84  va_start(ap2, fmt);
85  vfprintf(gLogFile->mFP, fmt, ap2);
86  va_end(ap2);
87  fflush(gLogFile->mFP);
88  }
89 }
virtual std::wstring getAppDataFolder()=0
Get the default application data folder.
Interface for unicode string class UString.
Definition: UString.h:35
HFCore::PFILE * p_fopen(const int8_hf *fileName, const int8_hf *access)
Open a file (within pak file or outside of it).
int32_hf p_fclose(HFCore::PFILE *file)
Closes and deletes a HFCore::PFILE.
const wchar_t * wc_str() const
Get the unicode widechar string.
Definition: UString.cpp:579
Interface useful for simple Logging.
virtual void createDirectory(const std::wstring sDirectoryPath)=0
Create directory.
#define NULL
Convenient define for 0.
Definition: HFDataTypes.h:42
const int8_hf * toUtf8() const
Get the utf8 encoded string.
Definition: UString.cpp:574
char int8_hf
A type definition for char.
Definition: HFDataTypes.h:329
static void log(const int8_hf *fmt,...)
Write formatted text to log file.
Definition: Log.cpp:79
void append(const UString &str)
Append str to internal string.
Definition: UString.cpp:365
FILE * mFP
Pointer to FILE.
Definition: PakInterface.h:150
static void destroy()
Destroy and free memory.
Definition: Log.cpp:73
Interface useful for generic directory and file operations.
Definition: FileDirUtils.h:57
Definition: Actor.h:34
Struct holding PakRecord, pointer to actual FILE and position.
Definition: PakInterface.h:146
static void init(FileDirUtils *fdu, const UString &publisherName, const UString &windowTitle)
Init log file.
Definition: Log.cpp:36
static std::string WStringToString(const std::wstring &string)
Convert std::wstring to std::string.
Definition: StringUtil.cpp:490
Interface for accessing pak files created with tool hfpak.
Interface useful for generic directory and file operations.