-
Notifications
You must be signed in to change notification settings - Fork 390
Expand file tree
/
Copy pathLog_Msg.cpp
More file actions
3260 lines (2882 loc) · 112 KB
/
Copy pathLog_Msg.cpp
File metadata and controls
3260 lines (2882 loc) · 112 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// We need this to get the status of ACE_NTRACE...
#include "ace/config-all.h"
// Turn off tracing for the duration of this file.
#if defined (ACE_NTRACE)
# undef ACE_NTRACE
#endif /* ACE_NTRACE */
#define ACE_NTRACE 1
#include "ace/ACE.h"
#include "ace/Thread_Manager.h"
#include "ace/Guard_T.h"
#include "ace/OS_NS_stdio.h"
#include "ace/OS_NS_errno.h"
#include "ace/OS_NS_sys_time.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_wchar.h"
#include "ace/OS_NS_signal.h"
#include "ace/os_include/os_typeinfo.h"
#if !defined (ACE_MT_SAFE) || (ACE_MT_SAFE != 0)
# include "ace/Object_Manager.h"
#endif /* ! ACE_MT_SAFE */
#if !defined (ACE_LACKS_IOSTREAM_TOTALLY)
// FUZZ: disable check_for_streams_include
# include "ace/streams.h"
#endif /* ! ACE_LACKS_IOSTREAM_TOTALLY */
#if defined (ACE_HAS_TRACE)
# include "ace/Trace.h"
#endif /* ACE_HAS_TRACE */
#include "ace/Log_Msg.h"
#include "ace/Log_Msg_Callback.h"
#include "ace/Log_Msg_IPC.h"
#include "ace/Log_Msg_NT_Event_Log.h"
#include "ace/Log_Msg_UNIX_Syslog.h"
#include "ace/Log_Record.h"
#include "ace/Recursive_Thread_Mutex.h"
#include "ace/Stack_Trace.h"
#include "ace/Atomic_Op.h"
#include <algorithm>
#if !defined (__ACE_INLINE__)
#include "ace/Log_Msg.inl"
#endif /* __ACE_INLINE__ */
#ifdef ACE_ANDROID
# include "ace/Log_Msg_Android_Logcat.h"
#endif
ACE_BEGIN_VERSIONED_NAMESPACE_DECL
ACE_ALLOC_HOOK_DEFINE(ACE_Log_Msg)
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
bool ACE_Log_Msg::key_created_ = 0;
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
defined (ACE_HAS_TSS_EMULATION)
static ACE_thread_key_t the_log_msg_tss_key;
ACE_thread_key_t *log_msg_tss_key ()
{
return &the_log_msg_tss_key;
}
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
#else
static ACE_Cleanup_Adapter<ACE_Log_Msg>* log_msg_cleanup = 0;
class ACE_Msg_Log_Cleanup: public ACE_Cleanup_Adapter<ACE_Log_Msg>
{
public:
virtual ~ACE_Msg_Log_Cleanup () {
if (this == log_msg_cleanup)
log_msg_cleanup = 0;
}
};
#endif /* ACE_MT_SAFE */
#if defined (ACE_WIN32)
# define ACE_LOG_MSG_SYSLOG_BACKEND ACE_Log_Msg_NT_Event_Log
#elif defined (ACE_ANDROID)
# define ACE_LOG_MSG_SYSLOG_BACKEND ACE_Log_Msg_Android_Logcat
#elif !defined (ACE_LACKS_UNIX_SYSLOG)
# define ACE_LOG_MSG_SYSLOG_BACKEND ACE_Log_Msg_UNIX_Syslog
#endif
// When doing ACE_OS::s[n]printf() calls in log(), we need to update
// the space remaining in the output buffer based on what's returned from
// the output function. If we could rely on more modern compilers, this
// would be in an unnamed namespace, but it's a macro instead.
// count is a size_t, len is an int and assumed to be non-negative.
#define ACE_UPDATE_COUNT(COUNT, LEN) \
do { if (static_cast<size_t> (LEN) > COUNT) COUNT = 0; \
else COUNT -= static_cast<size_t> (LEN); \
} while (0)
/// Instance count for Log_Msg - used to know when dynamically
/// allocated storage (program name and host name) can be safely
/// deleted.
int ACE_Log_Msg::instance_count_ = 0;
/**
* @class ACE_Log_Msg_Manager
*
* @brief Synchronize output operations.
*
* Provides global point of contact for all ACE_Log_Msg instances
* in a process.
*
* For internal use by ACE, only!
*/
class ACE_Log_Msg_Manager
{
public:
static ACE_Log_Msg_Backend *log_backend_;
static ACE_Log_Msg_Backend *custom_backend_;
static u_long log_backend_flags_;
static int init_backend (const u_long *flags = 0);
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
//FUZZ: disable check_for_lack_ACE_OS
static void close () ACE_GCC_DESTRUCTOR_ATTRIBUTE;
//FUZZ: enable check_for_lack_ACE_OS
static ACE_Recursive_Thread_Mutex *get_lock ();
private:
static ACE_Recursive_Thread_Mutex *lock_;
#endif /* ! ACE_MT_SAFE */
};
ACE_Log_Msg_Backend *ACE_Log_Msg_Manager::log_backend_ = 0;
ACE_Log_Msg_Backend *ACE_Log_Msg_Manager::custom_backend_ = 0;
#ifndef ACE_DEFAULT_LOG_BACKEND_FLAGS
# ifdef ACE_ANDROID
# define ACE_DEFAULT_LOG_BACKEND_FLAGS ACE_Log_Msg::SYSLOG
# else
# define ACE_DEFAULT_LOG_BACKEND_FLAGS 0
# endif
#endif
u_long ACE_Log_Msg_Manager::log_backend_flags_ = ACE_DEFAULT_LOG_BACKEND_FLAGS;
int ACE_Log_Msg_Manager::init_backend (const u_long *flags)
{
// If flags have been supplied, and they are different from the flags
// we had last time, then we may have to re-create the backend as a
// different type.
if (flags)
{
// Sanity check for custom backend.
if (ACE_BIT_ENABLED (*flags, ACE_Log_Msg::CUSTOM) &&
ACE_Log_Msg_Manager::custom_backend_ == 0)
{
return -1;
}
if ((ACE_BIT_ENABLED (*flags, ACE_Log_Msg::SYSLOG)
&& ACE_BIT_DISABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG))
|| (ACE_BIT_DISABLED (*flags, ACE_Log_Msg::SYSLOG)
&& ACE_BIT_ENABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG)))
{
delete ACE_Log_Msg_Manager::log_backend_;
ACE_Log_Msg_Manager::log_backend_ = 0;
}
ACE_Log_Msg_Manager::log_backend_flags_ = *flags;
}
if (ACE_Log_Msg_Manager::log_backend_ == 0)
{
#ifdef ACE_LOG_MSG_SYSLOG_BACKEND
// Allocate the ACE_Log_Msg_Backend instance.
if (ACE_BIT_ENABLED (ACE_Log_Msg_Manager::log_backend_flags_, ACE_Log_Msg::SYSLOG))
ACE_NEW_RETURN (ACE_Log_Msg_Manager::log_backend_,
ACE_LOG_MSG_SYSLOG_BACKEND,
-1);
else
#endif
ACE_NEW_RETURN (ACE_Log_Msg_Manager::log_backend_,
ACE_Log_Msg_IPC,
-1);
}
return 0;
}
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
ACE_Recursive_Thread_Mutex *ACE_Log_Msg_Manager::lock_ = 0;
ACE_Recursive_Thread_Mutex *
ACE_Log_Msg_Manager::get_lock ()
{
// This function is called by the first thread to create an ACE_Log_Msg
// instance. It makes the call while holding a mutex, so we don't have
// to grab another one here.
if (ACE_Log_Msg_Manager::lock_ == 0)
{
ACE_NEW_RETURN (ACE_Log_Msg_Manager::lock_,
ACE_Recursive_Thread_Mutex,
0);
}
if (init_backend () == -1)
return 0;
return ACE_Log_Msg_Manager::lock_;
}
void
ACE_Log_Msg_Manager::close ()
{
// Ugly, ugly, but don't know a better way.
delete ACE_Log_Msg_Manager::lock_;
ACE_Log_Msg_Manager::lock_ = 0;
delete ACE_Log_Msg_Manager::log_backend_;
ACE_Log_Msg_Manager::log_backend_ = 0;
// we are never responsible for custom backend
ACE_Log_Msg_Manager::custom_backend_ = 0;
}
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
defined (ACE_HAS_TSS_EMULATION)
/* static */
# if defined (ACE_HAS_THR_C_DEST)
# define LOCAL_EXTERN_PREFIX extern "C"
# else
# define LOCAL_EXTERN_PREFIX
# endif /* ACE_HAS_THR_C_DEST */
LOCAL_EXTERN_PREFIX
void
ACE_TSS_CLEANUP_NAME (void *ptr)
{
if (ptr != 0)
{
// Delegate to thr_desc if this not has terminated
ACE_Log_Msg *log_msg = (ACE_Log_Msg *) ptr;
if (log_msg->thr_desc () != 0)
log_msg->thr_desc ()->log_msg_cleanup (log_msg);
else
delete log_msg;
}
}
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
#endif /* ! ACE_MT_SAFE */
/* static */
int
ACE_Log_Msg::exists ()
{
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
defined (ACE_HAS_TSS_EMULATION)
void *tss_log_msg = 0; // The actual type is ACE_Log_Msg*, but we need this
// void to keep G++ from complaining.
// Get the tss_log_msg from thread-specific storage.
return ACE_Log_Msg::key_created_
&& ACE_Thread::getspecific (*(log_msg_tss_key ()), &tss_log_msg) != -1
&& tss_log_msg != 0;
# else
# error "Platform must support thread-specific storage if threads are used."
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
#else /* ! ACE_MT_SAFE */
return 1;
#endif /* ! ACE_MT_SAFE */
}
ACE_Log_Msg *
ACE_Log_Msg::instance ()
{
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
# if defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
defined (ACE_HAS_TSS_EMULATION)
// TSS Singleton implementation.
if (!ACE_Log_Msg::key_created_)
{
ACE_thread_mutex_t *lock =
reinterpret_cast<ACE_thread_mutex_t *> (
ACE_OS_Object_Manager::preallocated_object
[ACE_OS_Object_Manager::ACE_LOG_MSG_INSTANCE_LOCK]);
if (1 == ACE_OS_Object_Manager::starting_up())
//This function is called before ACE_OS_Object_Manager is
//initialized. So the lock might not be valid. Assume it's
//single threaded and so don't need the lock.
;
else
ACE_OS::thread_mutex_lock (lock);
if (!ACE_Log_Msg::key_created_)
{
// Allocate the Singleton lock.
ACE_Log_Msg_Manager::get_lock ();
if (ACE_Thread::keycreate (log_msg_tss_key (),
&ACE_TSS_CLEANUP_NAME) != 0)
{
if (1 == ACE_OS_Object_Manager::starting_up())
//This function is called before ACE_OS_Object_Manager is
//initialized. So the lock might not be valid. Assume it's
//single threaded and so don't need the lock.
;
else
ACE_OS::thread_mutex_unlock (lock);
return 0; // Major problems, this should *never* happen!
}
ACE_Log_Msg::key_created_ = true;
}
if (1 == ACE_OS_Object_Manager::starting_up())
//This function is called before ACE_OS_Object_Manager is
//initialized. So the lock might not be valid. Assume it's
//single threaded and so don't need the lock.
;
else
ACE_OS::thread_mutex_unlock (lock);
}
ACE_Log_Msg *tss_log_msg = 0;
void *temp = 0;
// Get the tss_log_msg from thread-specific storage.
if (ACE_Thread::getspecific (*(log_msg_tss_key ()), &temp) == -1)
return 0; // This should not happen!
tss_log_msg = static_cast <ACE_Log_Msg *> (temp);
// Check to see if this is the first time in for this thread.
if (tss_log_msg == 0)
{
// Allocate memory off the heap and store it in a pointer in
// thread-specific storage (on the stack...). The memory will
// always be freed by the thread rundown because of the TSS
// callback set up when the key was created.
ACE_NEW_RETURN (tss_log_msg,
ACE_Log_Msg,
0);
// Store the dynamically allocated pointer in thread-specific
// storage. It gets deleted via the ACE_TSS_cleanup function
// when the thread terminates.
if (ACE_Thread::setspecific (*(log_msg_tss_key()),
reinterpret_cast<void *> (tss_log_msg))
!= 0)
return 0; // Major problems, this should *never* happen!
}
return tss_log_msg;
# else
# error "Platform must support thread-specific storage if threads are used."
# endif /* ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION */
#else /* ! ACE_MT_SAFE */
// We don't have threads, we cannot call
// ACE_Log_Msg_Manager::get_lock () to initialize the logger
// callback, so instead we do it here.
if (ACE_Log_Msg_Manager::init_backend () == -1)
return 0;
// Singleton implementation.
if (log_msg_cleanup == 0)
{
ACE_NEW_RETURN (log_msg_cleanup, ACE_Msg_Log_Cleanup, 0);
// Register the instance for destruction at program termination.
ACE_Object_Manager::at_exit (log_msg_cleanup,
0,
typeid (*log_msg_cleanup).name ());
}
return &log_msg_cleanup->object ();
#endif /* ! ACE_MT_SAFE */
}
// Not inlined to help prevent having to include OS.h just to
// get ACELIB_DEBUG, et al, macros.
int
ACE_Log_Msg::last_error_adapter ()
{
return ACE_OS::last_error ();
}
// Sets the flag in the default priority mask used to initialize
// ACE_Log_Msg instances, as well as the current per-thread instance.
void
ACE_Log_Msg::enable_debug_messages (ACE_Log_Priority priority)
{
ACE_SET_BITS (ACE_Log_Msg::default_priority_mask_, priority);
ACE_Log_Msg *i = ACE_Log_Msg::instance ();
i->priority_mask (i->priority_mask () | priority);
}
// Clears the flag in the default priority mask used to initialize
// ACE_Log_Msg instances, as well as the current per-thread instance.
void
ACE_Log_Msg::disable_debug_messages (ACE_Log_Priority priority)
{
ACE_CLR_BITS (ACE_Log_Msg::default_priority_mask_, priority);
ACE_Log_Msg *i = ACE_Log_Msg::instance ();
i->priority_mask (i->priority_mask () & ~priority);
}
const ACE_TCHAR *
ACE_Log_Msg::program_name ()
{
return ACE_Log_Msg::program_name_;
}
/// Name of the local host.
const ACE_TCHAR *ACE_Log_Msg::local_host_ = 0;
/// Records the program name.
const ACE_TCHAR *ACE_Log_Msg::program_name_ = 0;
/// Default is to use stderr.
u_long ACE_Log_Msg::flags_ = ACE_DEFAULT_LOG_FLAGS;
/// Current offset of msg_[].
ptrdiff_t ACE_Log_Msg::msg_off_ = 0;
/// Default per-thread priority mask
/// By default, no priorities are enabled.
u_long ACE_Log_Msg::default_priority_mask_ = 0;
/// Default per-process priority mask
/// By default, all priorities are enabled.
u_long ACE_Log_Msg::process_priority_mask_ = LM_SHUTDOWN
| LM_TRACE
| LM_DEBUG
| LM_INFO
| LM_NOTICE
| LM_WARNING
| LM_STARTUP
| LM_ERROR
| LM_CRITICAL
| LM_ALERT
| LM_EMERGENCY;
void
ACE_Log_Msg::close ()
{
// This call needs to go here to avoid memory leaks.
ACE_MT (ACE_Log_Msg_Manager::close ());
// Please note that this will be called by a statement that is
// harded coded into the ACE_Object_Manager's shutdown sequence, in
// its destructor.
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0) && \
(defined (ACE_HAS_THREAD_SPECIFIC_STORAGE) || \
defined (ACE_HAS_TSS_EMULATION))
if (ACE_Log_Msg::key_created_)
{
ACE_thread_mutex_t *lock =
reinterpret_cast<ACE_thread_mutex_t *>
(ACE_OS_Object_Manager::preallocated_object
[ACE_OS_Object_Manager::ACE_LOG_MSG_INSTANCE_LOCK]);
if (lock)
ACE_OS::thread_mutex_lock (lock);
if (ACE_Log_Msg::key_created_)
{
// Clean up this ACE_Log_Msg instance and reset the TSS to
// prevent any future cleanup attempts via TSS mechanisms at
// thread exit. Otherwise in the event of a dynamic library
// unload of libACE, by a program not linked with libACE,
// ACE_TSS_cleanup will be invoked after libACE has been unloaded.
// See Bugzilla 2980 for lots of details.
void *temp = 0;
// Get the tss_log_msg from thread-specific storage.
if (ACE_Thread::getspecific (*(log_msg_tss_key ()), &temp) != -1
&& temp)
{
ACE_Log_Msg *tss_log_msg = static_cast <ACE_Log_Msg *> (temp);
// we haven't been cleaned up
ACE_TSS_CLEANUP_NAME(tss_log_msg);
if (ACE_Thread::setspecific(*(log_msg_tss_key()),
reinterpret_cast <void *>(0)) != 0)
ACE_OS::printf ("ACE_Log_Msg::close failed to ACE_Thread::setspecific to 0\n");
}
// The key is not needed any longer; ACE_Log_Msg is closing
// and will need to be reopened if this process wishes to use
// logging again. So delete the key.
ACE_Thread::keyfree (*(log_msg_tss_key()));
ACE_Log_Msg::key_created_ = false;
}
if (lock)
ACE_OS::thread_mutex_unlock (lock);
}
#endif /* (ACE_HAS_THREAD_SPECIFIC_STORAGE || ACE_HAS_TSS_EMULATION) && ACE_MT_SAFE */
}
void
ACE_Log_Msg::sync_hook (const ACE_TCHAR *prg_name)
{
ACE_LOG_MSG->sync (prg_name);
}
ACE_OS_Thread_Descriptor *
ACE_Log_Msg::thr_desc_hook ()
{
return ACE_LOG_MSG->thr_desc ();
}
// Call after a fork to resynchronize the PID and PROGRAM_NAME
// variables.
void
ACE_Log_Msg::sync (const ACE_TCHAR *prog_name)
{
ACE_TRACE ("ACE_Log_Msg::sync");
if (prog_name)
{
// Must free if already allocated!!!
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance()->free ((void *) ACE_Log_Msg::program_name_);
#else
ACE_OS::free ((void *) ACE_Log_Msg::program_name_);
#endif /* ACE_HAS_ALLOC_HOOKS */
ACE_Log_Msg::program_name_ = ACE_OS::strdup (prog_name);
}
ACE_Log_Msg::msg_off_ = 0;
}
u_long
ACE_Log_Msg::flags ()
{
ACE_TRACE ("ACE_Log_Msg::flags");
u_long result;
ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Log_Msg_Manager::get_lock (), 0));
result = ACE_Log_Msg::flags_;
return result;
}
void
ACE_Log_Msg::set_flags (u_long flgs)
{
ACE_TRACE ("ACE_Log_Msg::set_flags");
ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Log_Msg_Manager::get_lock ()));
ACE_SET_BITS (ACE_Log_Msg::flags_, flgs);
}
void
ACE_Log_Msg::clr_flags (u_long flgs)
{
ACE_TRACE ("ACE_Log_Msg::clr_flags");
ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Log_Msg_Manager::get_lock ()));
ACE_CLR_BITS (ACE_Log_Msg::flags_, flgs);
}
int
ACE_Log_Msg::acquire ()
{
ACE_TRACE ("ACE_Log_Msg::acquire");
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
return ACE_Log_Msg_Manager::get_lock ()->acquire ();
#else /* ! ACE_MT_SAFE */
return 0;
#endif /* ! ACE_MT_SAFE */
}
u_long
ACE_Log_Msg::priority_mask (u_long n_mask, MASK_TYPE mask_type)
{
u_long o_mask;
if (mask_type == THREAD)
{
o_mask = this->priority_mask_;
this->priority_mask_ = n_mask;
}
else
{
o_mask = ACE_Log_Msg::process_priority_mask_;
ACE_Log_Msg::process_priority_mask_ = n_mask;
}
return o_mask;
}
int
ACE_Log_Msg::release ()
{
ACE_TRACE ("ACE_Log_Msg::release");
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
return ACE_Log_Msg_Manager::get_lock ()->release ();
#else /* ! ACE_MT_SAFE */
return 0;
#endif /* ! ACE_MT_SAFE */
}
ACE_Log_Msg::ACE_Log_Msg ()
: status_ (0),
errnum_ (0),
linenum_ (0),
msg_ (0),
restart_ (1), // Restart by default...
ostream_ (nullptr),
ostream_refcount_ (0),
msg_callback_ (0),
trace_depth_ (0),
trace_active_ (false),
tracing_enabled_ (true), // On by default?
thr_desc_ (0),
priority_mask_ (default_priority_mask_),
timestamp_ (0)
{
// ACE_TRACE ("ACE_Log_Msg::ACE_Log_Msg");
ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Log_Msg_Manager::get_lock ()));
++instance_count_;
if (this->instance_count_ == 1)
ACE_Base_Thread_Adapter::set_log_msg_hooks (ACE_Log_Msg::init_hook,
ACE_Log_Msg::inherit_hook,
ACE_Log_Msg::close,
ACE_Log_Msg::sync_hook,
ACE_Log_Msg::thr_desc_hook);
this->conditional_values_.is_set_ = false;
char *timestamp = ACE_OS::getenv ("ACE_LOG_TIMESTAMP");
if (timestamp != 0)
{
// If variable is set or is set to date tag so we print date and time.
if (ACE_OS::strcmp (timestamp, "TIME") == 0)
{
this->timestamp_ = 1;
}
else if (ACE_OS::strcmp (timestamp, "DATE") == 0)
{
this->timestamp_ = 2;
}
}
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_ALLOCATOR_NORETURN (this->msg_, static_cast<ACE_TCHAR *>(ACE_Allocator::instance()->malloc(sizeof(ACE_TCHAR) * (ACE_MAXLOGMSGLEN+1))));
#else
ACE_NEW_NORETURN (this->msg_, ACE_TCHAR[ACE_MAXLOGMSGLEN+1]);
#endif /* ACE_HAS_ALLOC_HOOKS */
}
ACE_Log_Msg::~ACE_Log_Msg ()
{
#if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
int instance_count = 0;
// Only hold the guard while updating the instance_count_.
// If ACE_Log_Msg_Manager::close () is called, the lock will
// be deleted.
{
ACE_MT (ACE_GUARD (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Log_Msg_Manager::get_lock ()));
instance_count = --instance_count_;
}
// Release the guard.
#else /* ! ACE_MT_SAFE */
int instance_count = --instance_count_;
#endif /* ! ACE_MT_SAFE */
// If this is the last instance then cleanup. Only the last
// thread to destroy its ACE_Log_Msg instance should execute
// this block.
if (instance_count == 0)
{
// Destroy the message queue instance.
if (ACE_Log_Msg_Manager::log_backend_ != 0)
ACE_Log_Msg_Manager::log_backend_->close ();
// Close down custom backend
if (ACE_Log_Msg_Manager::custom_backend_ != 0)
ACE_Log_Msg_Manager::custom_backend_->close ();
# if defined (ACE_MT_SAFE) && (ACE_MT_SAFE != 0)
# if defined (ACE_HAS_TSS_EMULATION)
ACE_Log_Msg_Manager::close ();
# endif /* ACE_HAS_TSS_EMULATION */
# endif /* ACE_MT_SAFE */
if (ACE_Log_Msg::program_name_)
{
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance()->free ((void *) ACE_Log_Msg::program_name_);
#else
ACE_OS::free ((void *) ACE_Log_Msg::program_name_);
#endif /* ACE_HAS_ALLOC_HOOKS */
ACE_Log_Msg::program_name_ = 0;
}
if (ACE_Log_Msg::local_host_)
{
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance()->free ((void *) ACE_Log_Msg::local_host_);
#else
ACE_OS::free ((void *) ACE_Log_Msg::local_host_);
#endif /* ACE_HAS_ALLOC_HOOKS */
ACE_Log_Msg::local_host_ = 0;
}
}
this->cleanup_ostream ();
#if defined (ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance()->free(this->msg_);
#else
delete[] this->msg_;
#endif /* ACE_HAS_ALLOC_HOOKS */
}
void
ACE_Log_Msg::cleanup_ostream ()
{
if (this->ostream_refcount_)
{
if (--*this->ostream_refcount_ == 0)
{
#if defined (ACE_HAS_ALLOC_HOOKS)
this->ostream_refcount_->~Atomic_ULong();
ACE_Allocator::instance()->free(this->ostream_refcount_);
#else
delete this->ostream_refcount_;
#endif /* ACE_HAS_ALLOC_HOOKS */
#if defined (ACE_LACKS_IOSTREAM_TOTALLY)
ACE_OS::fclose (this->ostream_);
#else
delete this->ostream_;
#endif
this->ostream_ = nullptr;
}
this->ostream_refcount_ = 0;
}
}
// Open the sender-side of the message queue.
int
ACE_Log_Msg::open (const ACE_TCHAR *prog_name,
u_long flags,
const ACE_TCHAR *logger_key)
{
ACE_TRACE ("ACE_Log_Msg::open");
ACE_MT (ACE_GUARD_RETURN (ACE_Recursive_Thread_Mutex, ace_mon,
*ACE_Log_Msg_Manager::get_lock (), -1));
if (prog_name)
{
#if defined(ACE_HAS_ALLOC_HOOKS)
ACE_Allocator::instance()->free ((void *) ACE_Log_Msg::program_name_);
#else
ACE_OS::free ((void *) ACE_Log_Msg::program_name_);
#endif /* ACE_HAS_ALLOC_HOOKS */
ACE_ALLOCATOR_RETURN (ACE_Log_Msg::program_name_,
ACE_OS::strdup (prog_name),
-1);
}
else if (ACE_Log_Msg::program_name_ == 0)
{
ACE_ALLOCATOR_RETURN (ACE_Log_Msg::program_name_,
ACE_OS::strdup (ACE_TEXT ("<unknown>")),
-1);
}
int status = 0;
// Be sure that there is a message_queue_, with multiple threads.
ACE_MT (ACE_Log_Msg_Manager::init_backend (&flags));
// Always close the current handle before doing anything else.
if (ACE_Log_Msg_Manager::log_backend_ != 0)
ACE_Log_Msg_Manager::log_backend_->reset ();
if (ACE_Log_Msg_Manager::custom_backend_ != 0)
ACE_Log_Msg_Manager::custom_backend_->reset ();
// Note that if we fail to open the message queue the default action
// is to use stderr (set via static initialization in the
// Log_Msg.cpp file).
if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER)
|| ACE_BIT_ENABLED (flags, ACE_Log_Msg::SYSLOG))
{
// The SYSLOG backends (both NT and UNIX) can get along fine
// without the logger_key - they will default to prog_name if
// logger key is 0.
if (logger_key == 0 && ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER))
status = -1;
else
status = ACE_Log_Msg_Manager::log_backend_->open (logger_key);
if (status == -1)
ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::STDERR);
else
{
if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER))
ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER);
if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::SYSLOG))
ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::SYSLOG);
}
}
else if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER)
|| ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::SYSLOG))
{
// If we are closing down logger, redirect logging to stderr.
ACE_CLR_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER);
ACE_CLR_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::SYSLOG);
ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::STDERR);
}
if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::CUSTOM))
{
status =
ACE_Log_Msg_Manager::custom_backend_->open (logger_key);
if (status != -1)
ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::CUSTOM);
}
// Remember, ACE_Log_Msg::STDERR bit is on by default...
if (status != -1
&& ACE_BIT_ENABLED (flags,
ACE_Log_Msg::STDERR) == 0)
ACE_CLR_BITS (ACE_Log_Msg::flags_,
ACE_Log_Msg::STDERR);
// VERBOSE takes precedence over VERBOSE_LITE...
if (ACE_BIT_ENABLED (flags,
ACE_Log_Msg::VERBOSE_LITE))
ACE_SET_BITS (ACE_Log_Msg::flags_,
ACE_Log_Msg::VERBOSE_LITE);
else if (ACE_BIT_ENABLED (flags,
ACE_Log_Msg::VERBOSE))
ACE_SET_BITS (ACE_Log_Msg::flags_,
ACE_Log_Msg::VERBOSE);
if (ACE_BIT_ENABLED (flags,
ACE_Log_Msg::OSTREAM))
{
ACE_SET_BITS (ACE_Log_Msg::flags_,
ACE_Log_Msg::OSTREAM);
// Only set this to cerr if it hasn't already been set.
if (this->msg_ostream () == 0)
this->msg_ostream (ACE_DEFAULT_LOG_STREAM);
}
if (ACE_BIT_ENABLED (flags,
ACE_Log_Msg::MSG_CALLBACK))
ACE_SET_BITS (ACE_Log_Msg::flags_,
ACE_Log_Msg::MSG_CALLBACK);
if (ACE_BIT_ENABLED (flags,
ACE_Log_Msg::SILENT))
ACE_SET_BITS (ACE_Log_Msg::flags_,
ACE_Log_Msg::SILENT);
return status;
}
#ifndef ACE_LACKS_VA_FUNCTIONS
/**
* Valid Options (prefixed by '%', as in printf format strings) include:
* 'A': print an ACE_timer_t value
* 'a': exit the program at this point (var-argument is the exit status!)
* 'b': print a ssize_t value
* 'B': print a size_t value
* 'c': print a character
* 'C': print a character string
* 'i', 'd': print a decimal number
* 'I', indent according to nesting depth
* 'e', 'E', 'f', 'F', 'g', 'G': print a double
* 'l', print line number where an error occurred.
* 'M': print the name of the priority of the message.
* 'm': Return the message corresponding to errno value, e.g., as done by <strerror>
* 'N': print file name where the error occurred.
* 'n': print the name of the program (or "<unknown>" if not set)
* 'o': print as an octal number
* 'P': format the current process id
* 'p': format the appropriate errno message from sys_errlist, e.g., as done by <perror>
* 'Q': print out the uint64 number
* 'q': print out the int64 number
* '@': print a void* pointer (in hexadecimal)
* 'r': call the function pointed to by the corresponding argument
* 'R': print return status
* 'S': print out the appropriate signal message corresponding
* to var-argument, e.g., as done by strsignal()
* 's': format a character string
* 'T': print timestamp in hour:minute:sec:usec format.
* 'D': print timestamp in month/day/year hour:minute:sec:usec format.
* 't': print thread id (1 if single-threaded)
* 'u': print as unsigned int
* 'x': print as a hex number
* 'X': print as a hex number
* 'w': print a wide character
* 'W': print out a wide character string.
* 'z': print an ACE_OS::WChar character
* 'Z': print an ACE_OS::WChar character string
* ':': print a time_t value as an integral number
* '%': format a single percent sign, '%'
*/
ssize_t
ACE_Log_Msg::log (ACE_Log_Priority log_priority,
const ACE_TCHAR *format_str, ...)
{
ACE_TRACE ("ACE_Log_Msg::log");
// Start of variable args section.
va_list argp;
va_start (argp, format_str);
ssize_t const result = this->log (format_str,
log_priority,
argp);
va_end (argp);
return result;
}
#if defined (ACE_HAS_WCHAR)
/**
* Since this is the ANTI_TCHAR version, we need to convert
* the format string over.
*/
ssize_t
ACE_Log_Msg::log (ACE_Log_Priority log_priority,
const ACE_ANTI_TCHAR *format_str, ...)
{
ACE_TRACE ("ACE_Log_Msg::log");
// Start of variable args section.
va_list argp;
va_start (argp, format_str);
ssize_t const result = this->log (ACE_TEXT_ANTI_TO_TCHAR (format_str),
log_priority,
argp);
va_end (argp);
return result;
}
#endif /* ACE_HAS_WCHAR */
#endif /* ACE_LACKS_VA_FUNCTIONS */
#if defined ACE_HAS_STRERROR_R && defined ACE_LACKS_STRERROR
#define ACE_LOG_MSG_USE_STRERROR_R
#endif
ssize_t
ACE_Log_Msg::log (const ACE_TCHAR *format_str,
ACE_Log_Priority log_priority,
va_list argp,
ACE_Log_Category_TSS* category)
{
ACE_TRACE ("ACE_Log_Msg::log");
#if defined (ACE_LACKS_VA_FUNCTIONS)
ACE_UNUSED_ARG (log_priority);
ACE_UNUSED_ARG (format_str);
ACE_UNUSED_ARG (argp);
ACE_UNUSED_ARG (category);
ACE_NOTSUP_RETURN (-1);
#else
// External decls.
using PointerToFunction = void (*)(...);
// Check if there were any conditional values set.
bool const conditional_values = this->conditional_values_.is_set_;
// Reset conditional values.
this->conditional_values_.is_set_ = false;