Skip to content

Commit f7246b8

Browse files
committed
Minimal singleton logging system
(references #1169)
1 parent 2d73391 commit f7246b8

File tree

19 files changed

+571
-37
lines changed

19 files changed

+571
-37
lines changed

CMake.in.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ SET(OFBUILD_CUSTOM_CMAKE_VERSION "${CMAKE_MAJOR_VERSION}.${CMAKE_MINOR_VERSION}.
2323
SET(OPENFLUID_VERSION_MAJOR 2)
2424
SET(OPENFLUID_VERSION_MINOR 2)
2525
SET(OPENFLUID_VERSION_PATCH 0)
26-
SET(OPENFLUID_VERSION_STATUS "alpha101") # example: SET(OPENFLUID_VERSION_STATUS "rc1")
26+
SET(OPENFLUID_VERSION_STATUS "alpha102") # example: SET(OPENFLUID_VERSION_STATUS "rc1")
2727

2828
SET(OPENFLUID_VERSION_FULL "${OPENFLUID_VERSION_MAJOR}.${OPENFLUID_VERSION_MINOR}.${OPENFLUID_VERSION_PATCH}")
2929

@@ -211,7 +211,7 @@ SET(OFBUILD_GITASKUSER_ENVVAR_PREFIX "OFLD_GITASKUSER_")
211211

212212

213213
################### logfiles ###################
214-
214+
SET(OPENFLUID_INTERNAL_LOG_FILE "openfluid-internal.log")
215215
SET(OPENFLUID_MESSAGES_LOG_FILE "openfluid-messages.log")
216216

217217
SET(OPENFLUID_CUMULATIVE_PROFILE_FILE "openfluid-profile-cumulative.log")

cmake/OpenFLUIDHelpers.cmake.in

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,14 @@ FUNCTION(OPENFLUID_ADD_TEST)
149149
ENDIF()
150150
ENDIF()
151151

152+
# log path
153+
IF(_TEST_ENVVARS)
154+
SET(_TEST_ENVVARS "${_TEST_ENVVARS}\;OPENFLUID_LOG_PATH=${OFBUILD_TESTS_OUTPUT_DATA_DIR}")
155+
ELSE()
156+
SET(_TEST_ENVVARS "OPENFLUID_LOG_PATH=${OFBUILD_TESTS_OUTPUT_DATA_DIR}")
157+
ENDIF()
158+
159+
152160
SET_TESTS_PROPERTIES(${_ADD_TEST_NAME}
153161
PROPERTIES ENVIRONMENT ${_TEST_ENVVARS})
154162

src/apps/openfluid-devstudio/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
#include <openfluid/base/PreferencesManager.hpp>
5151
#include <openfluid/ui/common/UIHelpers.hpp>
5252
#include <openfluid/ui/common/OpenFLUIDSplashScreen.hpp>
53+
#include <openfluid/utils/InternalLogger.hpp>
5354
#include <openfluid/config.hpp>
5455
#include <openfluid/ui/config.hpp>
5556

@@ -64,6 +65,8 @@ int main(int argc, char** argv)
6465

6566
INIT_OPENFLUID_QT_APPLICATION_WITH_GUI(argc, argv);
6667

68+
openfluid::utils::log::setup();
69+
6770
do
6871
{
6972
openfluid::ui::common::OpenFLUIDSplashScreen Splash(

src/apps/openfluid/main.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
@file main.cpp
3535
3636
@author Jean-Christophe FABRE <[email protected]>
37+
@author Armel THÖNI <[email protected]>
3738
*/
3839

3940

@@ -45,6 +46,7 @@
4546
#include <openfluid/ware/TypeDefs.hpp>
4647
#include <openfluid/waresdev/WareSrcFactory.hpp>
4748
#include <openfluid/utils/CommandLineParser.hpp>
49+
#include <openfluid/utils/InternalLogger.hpp>
4850
#include <openfluid/tools/Filesystem.hpp>
4951
#include <openfluid/tools/IDHelpers.hpp>
5052

@@ -64,6 +66,10 @@ int main(int argc, char **argv)
6466
{
6567
INIT_OPENFLUID_APPLICATION();
6668

69+
// CMD app generates no internal log by default, since it can be used in pipelines that shoud not impact standard
70+
// log file, use LOG_PATH env var to generate an internal log recording
71+
openfluid::utils::log::setup(false);
72+
6773
#if defined OPENFLUID_OS_WINDOWS
6874
std::string PathSepText = "semicolon";
6975
#else

src/openfluid/config.hpp.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
@file config.hpp
3535

3636
@author Jean-Christophe FABRE <[email protected]>
37+
@author Armel THÖNI <[email protected]>
38+
3739
*/
3840

3941

@@ -147,6 +149,7 @@ const std::string TRANSLATIONS_FILEROOT = "@OPENFLUID_TRANSLATIONS_FILEROOT@";
147149

148150
// Log files
149151
const std::string MESSAGES_LOG_FILE = "@OPENFLUID_MESSAGES_LOG_FILE@";
152+
const std::string INTERNAL_LOG_FILE = "@OPENFLUID_INTERNAL_LOG_FILE@";
150153

151154

152155
// Profiling files

src/openfluid/tools/FileLogger.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@
3434
@file FileLogger.cpp
3535
3636
@author Jean-Christophe FABRE <[email protected]>
37+
@author Armel THÖNI <[email protected]>
3738
*/
3839

3940

41+
#include <openfluid/base/FrameworkException.hpp>
4042
#include <openfluid/tools/FileLogger.hpp>
4143

4244

@@ -93,9 +95,21 @@ std::string FileLogger::logTypeToString(LogType LType)
9395
// =====================================================================
9496

9597

96-
void FileLogger::init(const std::string& FilePath)
98+
void FileLogger::init(const std::string& FilePath, bool Overwrite)
9799
{
98-
m_LogFile.open(FilePath.c_str(),std::ios::out);
100+
if (Overwrite)
101+
{
102+
m_LogFile.open(FilePath.c_str(),std::ios::out);
103+
}
104+
else
105+
{
106+
m_LogFile.open(FilePath.c_str(),std::ios::app);
107+
}
108+
if (!m_LogFile.is_open())
109+
{
110+
throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
111+
"Log file opening failed: "+FilePath);
112+
}
99113
}
100114

101115

@@ -116,6 +130,19 @@ void FileLogger::close()
116130
// =====================================================================
117131

118132

133+
void FileLogger::flush()
134+
{
135+
if (m_LogFile.is_open())
136+
{
137+
m_LogFile.flush();
138+
}
139+
}
140+
141+
142+
// =====================================================================
143+
// =====================================================================
144+
145+
119146
void FileLogger::add(LogType LType, const std::string& Sender, const std::string& Msg)
120147
{
121148
std::scoped_lock<std::mutex> Lock(m_LogMutex);

src/openfluid/tools/FileLogger.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
@file FileLogger.hpp
3535
3636
@author Jean-Christophe FABRE <[email protected]>
37+
@author Armel THÖNI <[email protected]>
3738
*/
3839

3940

@@ -77,10 +78,12 @@ class OPENFLUID_API FileLogger
7778

7879
static std::string logTypeToString(LogType LType);
7980

80-
void init(const std::string& FilePath);
81+
void init(const std::string& FilePath, bool Overwrite=true);
8182

8283
void close();
8384

85+
void flush();
86+
8487
void add(LogType LType, const std::string& Context, const std::string& Msg);
8588

8689
unsigned int getInfosCount() const

src/openfluid/tools/Filesystem.cpp

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,10 @@ bool Filesystem::copyDirectoryContent(const std::filesystem::path& Source, const
307307
}
308308
catch (const std::filesystem::filesystem_error& e)
309309
{
310-
//TODO cleaner exception / logging management
311-
std::cerr << "Filesystem error: " << e.what() << std::endl;
312-
//return false; TOIMPL reenable this after test under windows
310+
throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
311+
std::string("Directory content copy failed: ")+e.what());
312+
//return false; TOIMPL double check this part with windows
313+
313314
}
314315
}
315316
return true;
@@ -475,7 +476,11 @@ std::string Filesystem::readFile(const openfluid::tools::Path& FileObj)
475476
std::copy(std::istream_iterator<char>{File >> std::noskipws},{},std::back_inserter(Content));
476477
File.close();
477478
}
478-
479+
else
480+
{
481+
throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
482+
"File opening failed: "+FileObj.toGeneric());
483+
}
479484
return Content;
480485
}
481486

@@ -493,6 +498,11 @@ void Filesystem::writeFile(const std::string& Content, const openfluid::tools::P
493498
File << Content;
494499
File.close();
495500
}
501+
else
502+
{
503+
throw openfluid::base::FrameworkException(OPENFLUID_CODE_LOCATION,
504+
"File opening failed: "+FileObj.toGeneric());
505+
}
496506
}
497507

498508

src/openfluid/ui/waresdev/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,6 @@ SET_TARGET_PROPERTIES(openfluid-ui-waresdev PROPERTIES VERSION "${OPENFLUID_VERS
9797
COMPILE_DEFINITIONS "OPENFLUID_DLL"
9898
DEFINE_SYMBOL "OPENFLUID_DLL_EXPORTS")
9999

100-
MESSAGE(STATUS "In ui/waresdev ${QT_VERSION_MAJOR}")
101-
102100
TARGET_LINK_LIBRARIES(openfluid-ui-waresdev
103101
openfluid-ui-common
104102
openfluid-waresdev

src/openfluid/ui/waresdev/GitUIProxy.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,9 @@
4545

4646
#include <openfluid/ui/waresdev/GitUIProxy.hpp>
4747
#include <openfluid/base/Environment.hpp>
48-
#include <openfluid/utils/ExternalProgram.hpp>
4948
#include <openfluid/tools/FilesystemPath.hpp>
49+
#include <openfluid/utils/ExternalProgram.hpp>
50+
#include <openfluid/utils/InternalLogger.hpp>
5051
#include <openfluid/config.hpp>
5152

5253

@@ -137,9 +138,9 @@ bool GitUIProxy::launchAuthCommand(QStringList Args, const QString& FromUrl, con
137138
{
138139
if (FromUrl.isEmpty() || ToPath.isEmpty())
139140
{
140-
// TODO for non GUI interface, find a way to redirect error/info
141-
//std::cout << "Empty remote url or empty destination path" << std::endl;
142-
emit error(tr("Empty remote url or empty destination path"));
141+
std::string ErrorMsg = "Empty remote url or empty destination path";
142+
openfluid::utils::log::error("Git", ErrorMsg);
143+
emit error(tr(ErrorMsg.c_str()));
143144
return false;
144145
}
145146

@@ -151,12 +152,15 @@ bool GitUIProxy::launchAuthCommand(QStringList Args, const QString& FromUrl, con
151152
openfluid::tools::FilesystemPath({WorkingDirectory.toStdString(), ".git", "index.lock"});
152153
if (GitIndexLockPath.exists())
153154
{
154-
//std::cout << "Can not operate, git lock detected:" << std::endl;
155-
emit error(tr("Can not operate, git lock detected."));
155+
std::string ErrorMsg = "Can not operate, git lock detected.";
156+
openfluid::utils::log::error("Git", ErrorMsg);
157+
emit error(tr(ErrorMsg.c_str()));
156158
return false;
157159
}
158160
else // HACK for debugging purpose, to remove before release
159161
{
162+
openfluid::utils::log::debug("Git",
163+
"git lock not detected here: "+WorkingDirectory.toStdString());
160164
emit info(tr("git lock not detected here:")+QString::fromStdString(WorkingDirectory.toStdString()));
161165
}
162166
}
@@ -202,30 +206,35 @@ bool GitUIProxy::launchAuthCommand(QStringList Args, const QString& FromUrl, con
202206
connect(mp_Process, SIGNAL(readyReadStandardOutput()), this, SLOT(processStandardOutput()));
203207
connect(mp_Process, SIGNAL(readyReadStandardError()), this, SLOT(processErrorOutputAsInfo()));
204208

205-
// TO BE USED IN INTERNAL LOGGING STACK AS "INFO"
206-
// std::cout << "PROCESS START:" << m_LocalGitProgram << " : ";
207-
// for (auto& p : Args)
208-
// {
209-
// std::cout << " " << p.toStdString() << std::endl;
210-
// }
211-
// std::cout << std::endl;
209+
std::string GitCommand = m_LocalGitProgram + " ";
210+
for (auto& p : Args)
211+
{
212+
GitCommand += " " + p.toStdString();
213+
}
214+
215+
openfluid::utils::log::info("Git", GitCommand);
212216
mp_Process->start(QString::fromStdString(m_LocalGitProgram),Args);
213217
if (!mp_Process->waitForStarted())
214218
{
215219
delete mp_Process;
216220
mp_Process = nullptr;
217-
//std::cout << "failed start:" << std::endl; TODO add to logging
221+
openfluid::utils::log::error("Git", "Git failed start");
218222
return false;
219223
}
220-
// std::cout << "PROCESS POST START" << std::endl;
221224
mp_Process->waitForFinished(-1);
222225
mp_Process->waitForReadyRead(-1);
223226

224227
QString Res = QString::fromUtf8(mp_Process->readAll());
228+
229+
std::string ResContent = Res.toStdString();
230+
if (ResContent.empty())
231+
{
232+
ResContent = "/EMPTY/";
233+
}
234+
openfluid::utils::log::debug("Git",
235+
"Git process content: "+ResContent);
225236

226-
// std::cout << "PROCESS READ" << Res.toStdString() << std::endl;
227237
int ErrCode = mp_Process->exitCode();
228-
// !std::cout << "EXIT:" << ErrCode << std::endl;
229238

230239
delete mp_Process;
231240
mp_Process = nullptr;
@@ -342,7 +351,7 @@ std::pair<bool, QString> GitUIProxy::removeSubmodule(const QString& MainPathStr
342351
{
343352
StandardOutput += tr("Submodule successfully removed");
344353
}
345-
//TODO for logging system std::cout << "OUT IN REMOVE:" << StandardOutput.toStdString() << std::endl;
354+
openfluid::utils::log::debug("Git", StandardOutput.toStdString());
346355
return std::pair(SummaryStatusCode, StandardOutput);
347356
}
348357

0 commit comments

Comments
 (0)