@@ -40,9 +40,21 @@ struct Static_Data {
4040 }
4141};
4242
43- static Static_Data g_log_data;
43+ static Storage< Static_Data> g_log_data;
4444static thread_local u64 g_log_indent = 0 ;
4545
46+ namespace detail {
47+
48+ Static_Init::Static_Init () noexcept {
49+ g_log_data.construct ();
50+ }
51+
52+ Static_Init::~Static_Init () noexcept {
53+ g_log_data.destruct ();
54+ }
55+
56+ } // namespace detail
57+
4658#ifdef RPP_OS_WINDOWS
4759
4860[[nodiscard]] String_View sys_error () noexcept {
@@ -147,23 +159,25 @@ void output(Level level, const Location& loc, String_View msg) noexcept {
147159 Thread::Id thread = Thread::this_id ();
148160 ::time_t timer = ::time (null);
149161
150- Thread::Lock lock (g_log_data.lock );
162+ {
163+ Thread::Lock lock (g_log_data->lock );
151164
152- String_View time = sys_time_string (timer);
165+ String_View time = sys_time_string (timer);
153166
154- printf (format_str, time.length (), time.data (), level_str, thread, loc.file .length (),
155- loc.file .data (), loc.line , g_log_indent * INDENT_SIZE, " " , msg.length (), msg.data ());
156- fflush (stdout);
167+ printf (format_str, time.length (), time.data (), level_str, thread, loc.file .length (),
168+ loc.file .data (), loc.line , g_log_indent * INDENT_SIZE, " " , msg.length (), msg.data ());
169+ fflush (stdout);
157170
158- for (auto & [_, callback] : g_log_data.callbacks ) {
159- callback (level, thread, timer, loc, msg);
171+ if (g_log_data->file ) {
172+ fprintf (g_log_data->file , format_str, time.length (), time.data (), level_str, thread,
173+ loc.file .length (), loc.file .data (), loc.line , g_log_indent * INDENT_SIZE, " " ,
174+ msg.length (), msg.data ());
175+ fflush (g_log_data->file );
176+ }
160177 }
161178
162- if (g_log_data.file ) {
163- fprintf (g_log_data.file , format_str, time.length (), time.data (), level_str, thread,
164- loc.file .length (), loc.file .data (), loc.line , g_log_indent * INDENT_SIZE, " " ,
165- msg.length (), msg.data ());
166- fflush (g_log_data.file );
179+ for (auto & [_, callback] : g_log_data->callbacks ) {
180+ callback (level, thread, timer, loc, msg);
167181 }
168182}
169183
@@ -178,17 +192,17 @@ Scope::~Scope() noexcept {
178192
179193[[nodiscard]] Token
180194subscribe (Function<void (Level, Thread::Id, Time, Location, String_View)> f) noexcept {
181- Thread::Lock lock (g_log_data. lock );
182- Token t = g_log_data. next ++;
183- g_log_data. callbacks .insert (t, rpp::move (f));
195+ Thread::Lock lock (g_log_data-> lock );
196+ Token t = g_log_data-> next ++;
197+ g_log_data-> callbacks .insert (t, rpp::move (f));
184198 return t;
185199}
186200
187201void unsubscribe (Token token) noexcept {
188- Thread::Lock lock (g_log_data. lock );
189- g_log_data. callbacks .erase (token);
190- if (g_log_data. callbacks .empty ()) {
191- g_log_data. callbacks .~Map ();
202+ Thread::Lock lock (g_log_data-> lock );
203+ g_log_data-> callbacks .erase (token);
204+ if (g_log_data-> callbacks .empty ()) {
205+ g_log_data-> callbacks .~Map ();
192206 }
193207}
194208
0 commit comments