2727#include < core/matrix.hpp>
2828#include < backend/backend_base.hpp>
2929#include < backend/matrix_base.hpp>
30+ #include < io/logger.hpp>
31+
32+ #include < fstream>
3033#include < iostream>
34+ #include < memory>
35+ #include < iomanip>
3136
3237#ifdef CUBOOL_WITH_CUDA
3338#include < cuda/cuda_backend.hpp>
@@ -41,6 +46,7 @@ namespace cubool {
4146
4247 std::unordered_set<class MatrixBase *> Library::mAllocated ;
4348 BackendBase* Library::mBackend = nullptr ;
49+ std::shared_ptr<class Logger > Library::mLogger = std::make_shared<DummyLogger>();
4450 bool Library::mRelaxedRelease = false ;
4551
4652 void Library::initialize (hints initHints) {
@@ -58,6 +64,8 @@ namespace cubool {
5864 if (!mBackend ->isInitialized ()) {
5965 delete mBackend ;
6066 mBackend = nullptr ;
67+
68+ mLogger ->logWarning (" Failed to initialize Cuda backend" );
6169 }
6270#endif
6371 }
@@ -71,6 +79,8 @@ namespace cubool {
7179 if (!mBackend ->isInitialized ()) {
7280 delete mBackend ;
7381 mBackend = nullptr ;
82+
83+ mLogger ->logWarning (" Failed to initialize Cpu fallback backend" );
7484 }
7585 }
7686#endif
@@ -101,6 +111,62 @@ namespace cubool {
101111 CHECK_RAISE_CRITICAL_ERROR (mBackend != nullptr || mRelaxedRelease , InvalidState, " Library is not initialized" );
102112 }
103113
114+ void Library::setupLogging (const char *logFileName, cuBool_Hints hints) {
115+ CHECK_RAISE_ERROR (logFileName != nullptr , InvalidArgument, " Null file name is not allowed" );
116+
117+ auto lofFile = std::make_shared<std::ofstream>();
118+
119+ lofFile->open (logFileName, std::ios::out);
120+
121+ if (!lofFile->is_open ()) {
122+ RAISE_ERROR (InvalidArgument, " Failed to create logging file" );
123+ }
124+
125+ // Create logger and setup filters && post-actions
126+ auto textLogger = std::make_shared<TextLogger>();
127+
128+ textLogger->addFilter ([=](Logger::Level level, const std::string& message) -> bool {
129+ bool all = hints == 0x0 || (hints & CUBOOL_HINT_LOG_ALL);
130+ bool error = hints & CUBOOL_HINT_LOG_ERROR;
131+ bool warning = hints & CUBOOL_HINT_LOG_WARNING;
132+
133+ return all ||
134+ (error && level == Logger::Level::Error) ||
135+ (warning && level == Logger::Level::Warning);
136+ });
137+
138+ textLogger->addOnLoggerAction ([=](size_t id, Logger::Level level, const std::string& message) {
139+ auto & file = *lofFile;
140+
141+ const auto idSize = 10 ;
142+ const auto levelSize = 20 ;
143+
144+ file << " [" << std::setw (idSize) << id << std::setw (-1 ) << " ]" ;
145+ file << " [" << std::setw (levelSize);
146+ switch (level) {
147+ case Logger::Level::Info:
148+ file << " Level::Info" ;
149+ break ;
150+ case Logger::Level::Warning:
151+ file << " Level::Warning" ;
152+ break ;
153+ case Logger::Level::Error:
154+ file << " Level::Error" ;
155+ break ;
156+ default :
157+ file << " Level::Always" ;
158+ }
159+ file << std::setw (-1 ) << " ] " ;
160+ file << message << std::endl << std::endl;
161+ });
162+
163+ // Assign new text logger
164+ mLogger = textLogger;
165+
166+ // Initial message
167+ mLogger ->logInfo (" *** cuBool::Logger file ***" );
168+ }
169+
104170 MatrixBase *Library::createMatrix (size_t nrows, size_t ncols) {
105171 CHECK_RAISE_ERROR (nrows > 0 , InvalidArgument, " Cannot create matrix with zero dimension" );
106172 CHECK_RAISE_ERROR (ncols > 0 , InvalidArgument, " Cannot create matrix with zero dimension" );
@@ -118,7 +184,11 @@ namespace cubool {
118184 }
119185
120186 void Library::handleError (const std::exception& error) {
121- std::cerr << " cuBool:Error: " << error.what () << std::endl;
187+ mLogger ->log (Logger::Level::Error, error.what ());
188+ }
189+
190+ class Logger * Library::getLogger () {
191+ return mLogger .get ();
122192 }
123193
124194}
0 commit comments