66// / This implements Omega logging initialization.
77//
88// ===----------------------------------------------------------------------===//
9-
10- #define _OMEGA_STRINGIFY (x ) #x
11- #define _OMEGA_TOSTRING (x ) _OMEGA_STRINGIFY(x)
12-
139#include " Logging.h"
1410#include " MachEnv.h"
1511#include < iostream>
1612#include < spdlog/sinks/basic_file_sink.h>
1713
14+ #define _OMEGA_STRINGIFY (x ) #x
15+ #define _OMEGA_TOSTRING (x ) _OMEGA_STRINGIFY(x)
16+
1817namespace OMEGA {
1918
2019// Function to pack log message
@@ -30,15 +29,17 @@ std::string _PackLogMsg(const char *file, int line, const std::string &msg) {
3029 return " [" + path + " :" + std::to_string (line) + " ] " + msg;
3130}
3231
33- std::vector<int > splitTasks (const std::string &str) {
32+ std::vector<int > splitTasks (const std::string &str, const OMEGA::I4 NumTasks ) {
3433 std::vector<int > Tasks;
3534 std::stringstream ss (str);
3635 std::string Task;
3736 int Start, Stop;
3837 char Dash;
3938
4039 if (str == " ALL" ) {
41- Tasks.push_back (-1 );
40+ for (int i = 0 ; i < NumTasks; ++i) {
41+ Tasks.push_back (i);
42+ }
4243 return Tasks;
4344 }
4445
@@ -59,25 +60,58 @@ std::vector<int> splitTasks(const std::string &str) {
5960 }
6061 }
6162
63+ if (std::find (Tasks.begin (), Tasks.end (), -1 ) != Tasks.end ()) {
64+ Tasks.clear ();
65+ for (int i = 0 ; i < NumTasks; ++i) {
66+ Tasks.push_back (i);
67+ }
68+ }
69+
6270 return Tasks;
6371}
6472
65- int initLogging (std::shared_ptr<spdlog::logger> Logger) {
73+ // return code: 1->enabled, 0->disabled, negative values->errors
74+ int initLogging (const OMEGA::MachEnv *DefEnv,
75+ std::shared_ptr<spdlog::logger> Logger) {
76+
77+ int RetVal = 0 ;
6678
67- int RetVal = 1 ;
79+ OMEGA::I4 TaskId = DefEnv->getMyTask ();
80+ OMEGA::I4 NumTasks = DefEnv->getNumTasks ();
6881
69- spdlog::set_default_logger (Logger);
82+ std::vector<int > Tasks =
83+ splitTasks (_OMEGA_TOSTRING (OMEGA_LOG_TASKS), NumTasks);
84+
85+ if (Tasks.size () > 0 &&
86+ (std::find (Tasks.begin (), Tasks.end (), TaskId) != Tasks.end ())) {
87+
88+ spdlog::set_default_logger (Logger);
89+
90+ spdlog::set_pattern (" [%n %l] %v" );
91+ spdlog::set_level (
92+ static_cast <spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL));
93+ spdlog::flush_on (spdlog::level::warn);
94+
95+ RetVal = 1 ; // log enabled
96+
97+ } else {
98+ spdlog::set_level (spdlog::level::off);
99+ RetVal = 0 ; // log disabled
100+ }
70101
71102 return RetVal;
72103}
73104
105+ // return code: 1->enabled, 0->disabled, negative values->errors
74106int initLogging (const OMEGA::MachEnv *DefEnv, std::string const &LogFilePath) {
75107
76- int RetVal;
108+ int RetVal = 0 ;
77109
78- OMEGA::I4 TaskId = DefEnv->getMyTask ();
110+ OMEGA::I4 TaskId = DefEnv->getMyTask ();
111+ OMEGA::I4 NumTasks = DefEnv->getNumTasks ();
79112
80- std::vector<int > Tasks = splitTasks (_OMEGA_TOSTRING (OMEGA_LOG_TASKS));
113+ std::vector<int > Tasks =
114+ splitTasks (_OMEGA_TOSTRING (OMEGA_LOG_TASKS), NumTasks);
81115
82116 if (Tasks.size () > 0 &&
83117 (std::find (Tasks.begin (), Tasks.end (), TaskId) != Tasks.end ())) {
@@ -89,27 +123,28 @@ int initLogging(const OMEGA::MachEnv *DefEnv, std::string const &LogFilePath) {
89123 auto NewLogFilePath = LogFilePath.substr (0 , dotPos) + " _" +
90124 std::to_string (TaskId) +
91125 LogFilePath.substr (dotPos);
92- initLogging ( spdlog::basic_logger_mt ( " * " , NewLogFilePath));
93-
126+ spdlog::set_default_logger (
127+ spdlog::basic_logger_mt ( " * " , NewLogFilePath));
94128 } else {
95- initLogging (spdlog::basic_logger_mt (" *" , LogFilePath));
129+ spdlog::set_default_logger (
130+ spdlog::basic_logger_mt (" *" , LogFilePath));
96131 }
97132
98133 spdlog::set_pattern (" [%n %l] %v" );
99134 spdlog::set_level (
100135 static_cast <spdlog::level::level_enum>(SPDLOG_ACTIVE_LEVEL));
101136 spdlog::flush_on (spdlog::level::warn);
102137
103- RetVal = 1 ;
138+ RetVal = 1 ; // log enalbed
104139
105140 } catch (spdlog::spdlog_ex const &Ex) {
106141 std::cout << " Log init failed: " << Ex.what () << std::endl;
107- RetVal = -1 ;
142+ RetVal = -1 ; // error occured
108143 }
109144
110145 } else {
111146 spdlog::set_level (spdlog::level::off);
112- RetVal = 0 ;
147+ RetVal = 0 ; // log disabled
113148 }
114149
115150 return RetVal;
0 commit comments