summaryrefslogtreecommitdiff
path: root/ace/Log_Msg.cpp
blob: 5378cbea14c68fc4c610177c03d0da06f517c8e5 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
// Log_Msg.cpp
// $Id$

#define ACE_BUILD_DLL
// We need this to get the status of ACE_NTRACE...
#include "ace/config.h"

// Turn off tracing for the duration of this file.
#if defined (ACE_NTRACE)
#undef ACE_NTRACE
#endif /* ACE_NTRACE */
#define ACE_NTRACE 1

// This must come first to avoid "order of include" problems...

#if !defined (ACE_HAS_INLINED_OSCALLS) && !defined (ACE_WIN32)
#define ACE_HAS_INLINED_OSCALLS
#include "ace/ACE.h"
#undef ACE_HAS_INLINED_OSCALLS
#else
#include "ace/ACE.h"
#endif /* !ACE_HAS_INLINED_OSCALLS */

#include "ace/Thread.h"
#include "ace/Synch.h"
#include "ace/Signal.h"
#include "ace/Set.h"

#if defined (ACE_HAS_UNICODE)
#define ACE_WSPRINTF(BUF,VALUE) ::wsprintf (BUF, "%S", VALUE)
#else
#define ACE_WSPRINTF(BUF,VALUE) ACE_OS::sprintf (BUF, "%s", VALUE)
#endif /* ACE_HAS_UNICODE */

// IPC conduit between sender and client daemon.  This should be
// included in the <ACE_Log_Msg> class, but due to "order of include"
// problems it can't be...
#if defined (ACE_WIN32)
#include "ace/SPIPE_Connector.h"
static ACE_SPIPE_Stream message_queue_;
#else
#include "ace/FIFO_Send_Msg.h"
static ACE_FIFO_Send_Msg message_queue_;
#endif /* ACE_WIN32 */

ACE_ALLOC_HOOK_DEFINE(ACE_Log_Msg)

#if !defined (VXWORKS) && defined (ACE_MT_SAFE)
static int key_created_ = 0;
static ACE_thread_key_t key_;
#endif /* !VXWORKS && ACE_MT_SAFE */

// This is only needed here because we can't afford to call
// ACE_LOG_MSG->instance() from within ACE_Log_Msg::instance() or else
// we will recurse infinitely!

#define ACE_NEW_RETURN_I(POINTER,CONSTRUCTOR,RET_VAL) \
   do { POINTER = new CONSTRUCTOR; \
     if (POINTER == 0) { errno = ENOMEM; return RET_VAL; } \
     } while (0)

#if defined (ACE_MT_SAFE)

typedef ACE_Unbounded_Set<ACE_Log_Msg *> ACE_Log_Msg_Set;

class ACE_Log_Msg_Manager
  // = TITLE
  //      Synchronize output operations.
{
public:
  static ACE_Thread_Mutex *get_lock (void);

#if defined (VXWORKS)
  static void atexit (WIND_TCB *);
#else
  static void atexit (void);

  static void insert (ACE_Log_Msg *);
  static int remove (ACE_Log_Msg *);
  // Remove returns the number of ACE_Log_Msg instances remaining.
  // When there are none left it returns 0.
#endif /* VXWORKS */

private:
  static ACE_Thread_Mutex *lock_;

  // Holds a list of all <ACE_Log_Msg> instances.  <instances_>
  // requires global construction/destruction.  If that's a problem,
  // could change it to a pointer and allocate it dynamically when
  // lock_ is allocated.
#if !defined (VXWORKS)
  static ACE_Log_Msg_Set instances_;
#endif /* VXWORKS */
};

#if defined (ACE_HAS_SIG_C_FUNC)
extern "C" void 
ace_log_msg_atexit (void)
{
  ACE_Log_Msg_Manager::atexit ();
}
#endif /* ACE_HAS_SIG_C_FUNC */

ACE_Thread_Mutex *ACE_Log_Msg_Manager::lock_ = 0;

ACE_Thread_Mutex *
ACE_Log_Msg_Manager::get_lock (void)
{
  if (ACE_Log_Msg_Manager::lock_ == 0)
    {
      ACE_NO_HEAP_CHECK;

      ACE_NEW_RETURN_I (ACE_Log_Msg_Manager::lock_, ACE_Thread_Mutex, 0);
    }

  return ACE_Log_Msg_Manager::lock_;
}

#if defined (VXWORKS)
void
ACE_Log_Msg_Manager::atexit (WIND_TCB *tcb)
{
  // The task is exiting, so its ACE_Log_Msg instance.
  delete (ACE_Log_Msg *) tcb->spare1;

  // Ugly, ugly, but don't know a better way.
  delete ACE_Log_Msg_Manager::lock_;
  ACE_Log_Msg_Manager::lock_ = 0;
}
#else
ACE_Log_Msg_Set ACE_Log_Msg_Manager::instances_;

void
ACE_Log_Msg_Manager::insert (ACE_Log_Msg *log_msg)
{
  ACE_Log_Msg_Manager::instances_.insert (log_msg);
}

int
ACE_Log_Msg_Manager::remove (ACE_Log_Msg *log_msg)
{
  ACE_Log_Msg_Manager::instances_.remove (log_msg);

  return ACE_Log_Msg_Manager::instances_.size ();
}

void
ACE_Log_Msg_Manager::atexit (void)
{
  // The program is exiting, so delete all ACE_Log_Msg instances.
  ACE_Unbounded_Set_Iterator <ACE_Log_Msg *> i 
    (ACE_Log_Msg_Manager::instances_);

  for (ACE_Log_Msg **log_msg;
       i.next (log_msg) != 0;
       )
    {
      // Advance the iterator first because it needs to read the next
      // field of the ACE_Set_Node that will be deleted as a result of
      // the following call to remove the node.
      i.advance ();

      // Causes a call to <ACE_Log_Msg_Manager::remove> via the
      // destructor of <ACE_Log_Msg>.
      delete *log_msg;
    }

  ACE_OS::thr_keyfree (key_);

  // Ugly, ugly, but don't know a better way.
  delete ACE_Log_Msg_Manager::lock_;
  ACE_Log_Msg_Manager::lock_ = 0;
}
#endif /* ! VXWORKS */

/* static */
#if defined (ACE_HAS_THR_C_FUNC)
extern "C" 
#endif /* ACE_HAS_THR_C_FUNC */
void
ACE_TSS_cleanup (void *ptr)
{
  delete (ACE_Log_Msg *) ptr;
}
#endif /* ACE_MT_SAFE */

/* static */
int 
ACE_Log_Msg::exists (void)
{
#if defined (ACE_MT_SAFE)
#if defined (VXWORKS)
  // Get the tss_log_msg from thread-specific storage, using one of
  // the "spare" fields in the task control block.  Note that no locks
  // are required here since this is within our thread context.  This
  // assumes that the sizeof the spare1 field is the same size as a
  // pointer; it is (it's an int) in VxWorks 5.2-5.3.
  ACE_Log_Msg *&tss_log_msg = (ACE_Log_Msg *&) taskIdCurrent->spare1;

  // Check to see if this is the first time in for this thread.  This
  // assumes that the spare1 field in the task control block is
  // initialized to 0, which holds true for VxWorks 5.2-5.3.
  return (tss_log_msg == 0);
#elif !defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
#error "Platform must support thread-specific storage if threads are used..."
#else
  ACE_Log_Msg *tss_log_msg = 0;

  // Get the tss_log_msg from thread-specific storage.  
  return key_created_
    && ACE_OS::thr_getspecific (key_, (void **) &tss_log_msg) != -1
    && tss_log_msg;

#endif /* VXWORKS || ACE_HAS_THREAD_SPECIFIC_STORAGE */
#else 
  return 1;
#endif /* defined (ACE_MT_SAFE) */
}

ACE_Log_Msg *
ACE_Log_Msg::instance (void)
{
#if defined (ACE_MT_SAFE)
#if defined (VXWORKS)
  // Get the tss_log_msg from thread-specific storage, using one of
  // the "spare" fields in the task control block.  Note that no locks
  // are required here since this is within our thread context.  This
  // assumes that the sizeof the spare1 field is the same size as a
  // pointer; it is (it's an int) in VxWorks 5.2-5.3.
  ACE_Log_Msg *&tss_log_msg = (ACE_Log_Msg *&) taskIdCurrent->spare1;

  // Check to see if this is the first time in for this thread.  This
  // assumes that the spare1 field in the task control block is
  // initialized to 0, which holds true for VxWorks 5.2-5.3.
  if (tss_log_msg == 0)
    {
      // Allocate the Singleton lock.  Note that this isn't
      // thread-safe.
      ACE_Log_Msg_Manager::get_lock ();

      // Register cleanup handler.
      ::taskDeleteHookAdd ((FUNCPTR) ACE_Log_Msg_Manager::atexit);

      // Allocate memory off the heap and store it in a pointer in
      // thread-specific storage, i.e., in the task control block.
      ACE_NEW_RETURN_I (tss_log_msg, ACE_Log_Msg, 0);
    }

  return tss_log_msg;
#elif !defined (ACE_HAS_THREAD_SPECIFIC_STORAGE)
#error "Platform must support thread-specific storage if threads are used..."
#else
  // TSS Singleton implementation.

  if (key_created_ == 0)
    {
      // Synchronize Singleton creation (note that this may lose big
      // if the compiler doesn't perform thread-safe initialization of
      // local statics...).
      static ACE_Thread_Mutex keylock_;
      ACE_thread_mutex_t &lock = (ACE_thread_mutex_t &) keylock_.lock ();

      ACE_OS::thread_mutex_lock (&lock);

      if (key_created_ == 0)
	{
	  // Allocate the Singleton lock.
	  ACE_Log_Msg_Manager::get_lock ();

	  {
	    ACE_NO_HEAP_CHECK;
	    if (ACE_OS::thr_keycreate (&key_,
				       &ACE_TSS_cleanup) != 0)
	      {
	        ACE_OS::thread_mutex_unlock (&lock);
	        return 0; // Major problems, this should *never* happen!
	      }
	  }

	  // Register cleanup handler.
#if defined (ACE_HAS_SIG_C_FUNC)
	  ::atexit (ace_log_msg_atexit);
#else
	  ::atexit (ACE_Log_Msg_Manager::atexit);
#endif /* ACE_HAS_SIG_C_FUNC */
	  key_created_ = 1;
	}
      ACE_OS::thread_mutex_unlock (&lock);
    }

  ACE_Log_Msg *tss_log_msg = 0;

  // Get the tss_log_msg from thread-specific storage.  
  if (ACE_OS::thr_getspecific (key_, 
			       (void **) &tss_log_msg) == -1)
    return 0; // This should not happen!

  // 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...).

      // Must protect access to lock managers list
      ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *ACE_Log_Msg_Manager::get_lock (), 0));

      // Stop heap checking, the memory will always be freed in
      // atexit. This prevents from getting these blocks reported as
      // memory leaks
      {
	ACE_NO_HEAP_CHECK;

	ACE_NEW_RETURN_I (tss_log_msg, ACE_Log_Msg, 0);
	ACE_Log_Msg_Manager::insert (tss_log_msg);

	// Store the dynamically allocated pointer in thread-specific
	// storage.
	if (ACE_OS::thr_setspecific (key_, (void *) tss_log_msg) != 0)
	  return 0; // Major problems, this should *never* happen!
      }
    }

  return tss_log_msg;
#endif /* VXWORKS || ACE_HAS_THREAD_SPECIFIC_STORAGE */
#else 
  // Singleton implementation.
  static ACE_Log_Msg log_msg;

  return &log_msg;
#endif /* defined (ACE_MT_SAFE) */
}
#undef ACE_NEW_RETURN_I

// Name of the local host.
const char *ACE_Log_Msg::local_host_ = 0;

// Records the program name. 
const char *ACE_Log_Msg::program_name_ = 0;

// Default is to use stderr.
u_long ACE_Log_Msg::flags_ = ACE_Log_Msg::STDERR;

// Process id of the current process. 
pid_t ACE_Log_Msg::pid_ = -1;

// Current offset of msg_[].
int ACE_Log_Msg::msg_off_ = 0;

// Call after a fork to resynchronize the PID and PROGRAM_NAME
// variables.

void
ACE_Log_Msg::sync (const char *prog_name)
{
  ACE_TRACE ("ACE_Log_Msg::sync");

#if defined (VXWORKS)
  // To avoid memory leak, don't copy the input string.  It had better
  // be in the text segment!
  ACE_Log_Msg::program_name_ = prog_name;
#else
  if (prog_name)
    {
      // Must free if already allocated!!!
      ACE_OS::free ((void *) ACE_Log_Msg::program_name_);

      // Stop heap checking, block will be freed in <atexit>.  Heap
      // checking state will be restored when the block is left.
      {
	ACE_NO_HEAP_CHECK;
      
	ACE_Log_Msg::program_name_ = ACE_OS::strdup (prog_name);
      }	  
    }
#endif /* VXWORKS */

  ACE_Log_Msg::pid_ = ACE_OS::getpid ();  
  ACE_Log_Msg::msg_off_ = 0;
}

u_long
ACE_Log_Msg::flags (void)
{
  ACE_TRACE ("ACE_Log_Msg::flags");
  u_long result;
  ACE_MT (ACE_GUARD_RETURN (ACE_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_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_Thread_Mutex, ace_mon, *ACE_Log_Msg_Manager::get_lock ()));

  ACE_CLR_BITS (ACE_Log_Msg::flags_, flgs);
}

int
ACE_Log_Msg::acquire (void)
{
  ACE_TRACE ("ACE_Log_Msg::acquire");
#if defined (ACE_MT_SAFE)
  return ACE_Log_Msg_Manager::get_lock ()->acquire ();
#else
  return 0;
#endif /* ACE_MT_SAFE */
}

u_long
ACE_Log_Msg::priority_mask (u_long n_mask) 
{
  u_long o_mask = this->priority_mask_;
  this->priority_mask_ = n_mask;
  return o_mask;
}

u_long
ACE_Log_Msg::priority_mask (void)
{
  return this->priority_mask_;
}

int
ACE_Log_Msg::release (void)
{
  ACE_TRACE ("ACE_Log_Msg::release");

#if defined (ACE_MT_SAFE)
  return ACE_Log_Msg_Manager::get_lock ()->release ();
#else
  return 0;
#endif /* ACE_MT_SAFE */
}

ACE_Log_Msg::ACE_Log_Msg (void)
  : status_ (0),
    errnum_ (0),
    linenum_ (0),
    restart_ (1),  // Restart by default...
    ostream_ (0),
    trace_depth_ (0),
    thr_handle_ (0),
    trace_active_ (0),
    tracing_enabled_ (1), // On by default?
    thr_state_ (0),
    priority_mask_ (LM_SHUTDOWN // By default, all priorities are enabled.
		    | LM_TRACE 
		    | LM_DEBUG
		    | LM_INFO
		    | LM_NOTICE
		    | LM_WARNING
		    | LM_STARTUP
		    | LM_ERROR
		    | LM_CRITICAL
		    | LM_ALERT
		    | LM_EMERGENCY)
{
  // ACE_TRACE ("ACE_Log_Msg::ACE_Log_Msg");
}

ACE_Log_Msg::~ACE_Log_Msg (void)
{
  // On VxWorks, there is no static Log_Msg_Set.  And, the
  // <program_name_> and <local_host_> strings weren't duplicated, in
  // order to avoid memory leaks, because this destructor can't tell
  // when the last thread in the program exits.
#if !defined (VXWORKS) && defined (ACE_MT_SAFE)
  ACE_MT (ACE_GUARD (ACE_Thread_Mutex, ace_mon, *ACE_Log_Msg_Manager::get_lock ()));
  
  // If this is the last instance then cleanup.
  if (ACE_Log_Msg_Manager::remove (this) == 0)
    {
      if (ACE_Log_Msg::program_name_) 
	{
	  ACE_OS::free ((void *) ACE_Log_Msg::program_name_);
	  ACE_Log_Msg::program_name_ = 0;
	}

      if (ACE_Log_Msg::local_host_) 
	{
	  ACE_OS::free ((void *) ACE_Log_Msg::local_host_);
	  ACE_Log_Msg::local_host_ = 0;
	}
    }
#endif /* !VXWORKS && ACE_MT_SAFE */
}

// Open the sender-side of the Message ACE_Queue.

int
ACE_Log_Msg::open (const char *prog_name, 
		   u_long flags, 
		   LPCTSTR logger_key)
{
  ACE_TRACE ("ACE_Log_Msg::open");
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *ACE_Log_Msg_Manager::get_lock (), -1));

#if defined (VXWORKS)
  // To avoid memory leak, don't copy the input string.  It had better
  // be in the text segment!
  ACE_Log_Msg::program_name_ = prog_name;
#else
  if (prog_name)
    {
      ACE_OS::free ((void *) ACE_Log_Msg::program_name_);

      // Stop heap checking, block will be freed in <atexit>.
      {
	ACE_NO_HEAP_CHECK;
     
	ACE_Log_Msg::program_name_ = ACE_OS::strdup (prog_name);
      }
    }
#endif /* VXWORKS */

  int status = 0;

  // Note that if we fail to open the message queue the default action
  // is to use stderr (set via static initialization in the
  // ACE_Log_Msg.C file).
  
  if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::LOGGER))
    {
      if (logger_key == 0)
	status = -1;
      else
#if defined (ACE_WIN32)
	{
	  ACE_SPIPE_Connector con;
	  status = con.connect (message_queue_, ACE_SPIPE_Addr (logger_key));
	}
#else
	status = message_queue_.open (logger_key);
#endif /* ACE_WIN32 */

      if (status == -1)
	ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::STDERR);
      else
	ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER);
    }

  // 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);

  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 (ACE_LOG_MSG->msg_ostream () == 0)
	ACE_LOG_MSG->msg_ostream (&cerr);
    }
  if (ACE_BIT_ENABLED (flags, ACE_Log_Msg::SILENT))
    ACE_SET_BITS (ACE_Log_Msg::flags_, ACE_Log_Msg::SILENT);

  return status;
}

// Valid Options (prefixed by '%', as in printf format strings) include:
//   'a': exit the program at this point (var-argument is the exit status!)
//   'c': print a character
//   'i', 'd': print a decimal number
//   'e', 'E', 'f', 'F', 'g', 'G': print a double
//   'l', print line number where an error occurred.
//   '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 value from sys_errlist
//   'r': call the function pointed to by the corresponding argument
//   'R': print return status
//   'S': format the appropriate _sys_siglist entry corresponding to var-argument.
//   's': format a character string
//   'T': print timestamp in hour:minute:sec:usec format.
//   't': print thread id (1 if single-threaded)
//   'u': print as unsigned int
//   'X', 'x': print as a hex number
//   '%': format a single percent sign, '%' 

ssize_t
ACE_Log_Msg::log (ACE_Log_Priority log_priority, 
		  const char *format_str, ...)
{ 
  ACE_TRACE ("ACE_Log_Msg::log");

  // Start of variable args section. 
  va_list argp;		

  va_start (argp, format_str);

  int result = this->log (format_str, log_priority, argp);

  va_end (argp);

  return result;
}

ssize_t
ACE_Log_Msg::log (const char *format_str,
		  ACE_Log_Priority log_priority, 
		  va_list argp)
{ 
  ACE_TRACE ("ACE_Log_Msg::log");
  // External decls. 

  extern int sys_nerr;
  typedef void (*PTF)(...);

  // Only print the message if <priority_mask_> hasn't been reset to
  // exclude this logging priority.

  if (ACE_BIT_DISABLED (this->priority_mask_, log_priority))
    return 0;

  ACE_Log_Record log_record (log_priority, 
			     ACE_OS::gettimeofday (),
			     this->getpid ());
  char *bp = this->msg ();
  int abort_prog = 0;
  int exit_value = 0;
  int result = 0;
  char *format = ACE_OS::strdup (format_str);
  char *save_p = format; // Remember pointer for ACE_OS::free()

  if (format == 0)
    return -1;

  if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::VERBOSE))
    {
      // Prepend the program name onto this message 

      if (ACE_Log_Msg::program_name_ != 0)
	{
	  for (const char *s = ACE_Log_Msg::program_name_; (*bp = *s) != '\0'; s++)
	    bp++;

	  *bp++ = '|';
	}
    }

  while (*format != '\0')
    {
      // Copy input to output until we encounter a %, however a
      // % followed by another % is not a format specification. 

      if (*format != '%')
	*bp++ = *format++;
      else if (format[1] == '%') // An "escaped" '%' (just print one '%').
	{
	  *bp++ = *format++;	// Store first % 
	  format++;	// but skip second % 
	}
      else
	{
	  char c;		// high use character	
	  char *fp;		// local format pointer	
	  int  wpc;		// width/precision cnt  
	  const int CONTINUE = 0;
	  const int SKIP_SPRINTF = -1;  // We must skip the sprintf phase 
	  const int SKIP_NUL_LOCATE = -2; // Skip locating the NUL character 
	  int type = CONTINUE;	// conversion type	
	  int  w[2];		// width/precision vals 

	  // % starts a format specification that ends with one of
	  // "arnPpSsdciouxXfFeEgG".  An optional width and/or precision
	  // (indicated by an "*") may be encountered prior to the
	  // nend of the specification, each consumes an int arg.
	  // A call to sprintf() does the actual conversion. 

	  fp = format++;	// Remember beginning of format.   
	  wpc = 0;		// Assume no width/precision seen. 

	  while (type == CONTINUE)
	    {
	      switch (*format++)
		{
		case 'a': // Abort program after handling all of format string. 
		  type = SKIP_SPRINTF;
		  abort_prog = 1; 
		  exit_value = va_arg (argp, int); 
		  ACE_OS::sprintf (bp, ""); // Make sure to NUL-terminate this...
		  break;
		case 'l':
		  ACE_OS::sprintf (bp, "%d", this->linenum ());
		  type = SKIP_SPRINTF;
		  break;
		case 'N':
		  {
		    // @@ UNICODE
		    const char *file = this->file ();
		    ACE_OS::sprintf (bp, "%s", file ? file : "<unknown file>");
		    type = SKIP_SPRINTF;
		    break;
		  }
		case 'n': // Print the name of the program. 
		  type = SKIP_SPRINTF;
		  // @@ UNICODE
		  ACE_OS::strcpy (bp, ACE_Log_Msg::program_name_ ? 
				  ACE_Log_Msg::program_name_ : "<unknown>");
		  break;
                case 'P': // Format the current process id.
		  type = SKIP_SPRINTF;
		  ACE_OS::sprintf (bp, "%d", this->getpid ());
		  break;
		case 'p': // Format the string assocated with the value of errno. 
		  {
		    type = SKIP_SPRINTF;
		    errno = ACE::map_errno (this->errnum ());
		    if (errno >= 0 && errno < sys_nerr)
		      ACE_OS::sprintf (bp, "%s: %s", 
				       va_arg (argp, char *), strerror (errno));
		    else
		      {
#if defined (ACE_WIN32)			
			LPTSTR lpMsgBuf = 0;
 
			::FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM,
					 NULL,
					 errno,
					 MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
					 (LPTSTR) &lpMsgBuf,
					 0,
					 NULL);

			// If we don't get a valid response from
			// <FormatMessage>, we'll assume this is a
			// WinSock error and so we'll try to convert
			// it into a string.  If this doesn't work it
			// returns "unknown error" which is fine for
			// our purposes.
			if (lpMsgBuf == 0)
			  {
			    const char *message = ACE::sock_error (errno);
			    ACE_OS::sprintf (bp, "%s: %s", 
					     va_arg (argp, const char *), message);
			  }
			else
			  {
			    ACE_OS::sprintf (bp, "%s: %s", 
					     va_arg (argp, char *), lpMsgBuf);
			    // Free the buffer.
			    ::LocalFree (lpMsgBuf);
			  }
#else
			ACE_OS::sprintf (bp, "%s: <unknown error> = %d",
					 va_arg (argp, char *), errno);
#endif /* ACE_WIN32 */		       
		      }
		    break;
		  }
		case 'R': // Format the return status of the operation.
		  this->op_status (va_arg (argp, int));
		  ACE_OS::sprintf (bp, "%d", this->op_status ());
		  break;
		case 'r': // Run (invoke) this subroutine. 
		  {
		    int osave = ACE_Log_Msg::msg_off_;
 
		    if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::SILENT))
		      *bp++ = '{';
		    ACE_Log_Msg::msg_off_ =  bp - this->msg_;
 
		    type = SKIP_SPRINTF;
		    (*va_arg (argp, PTF))(); 

		    if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::SILENT)) 
		      {
			bp += ACE_OS::strlen (bp);
			*bp++ =  '}';
		      }
		    *bp = '\0';

		    ACE_Log_Msg::msg_off_ = osave;
		    break;
		  }
		case 'S': // format the string associated with this signal number. 
		  {
		    int sig = va_arg (argp, int);
		    type = SKIP_SPRINTF;
#if defined (ACE_HAS_SYS_SIGLIST)
		    if (sig >= 0 && sig < NSIG)
		      ACE_OS::strcpy (bp, _sys_siglist[sig]);
		    else
		      ACE_OS::sprintf (bp, "<unknown signal> %d", sig);
#else
		    ACE_OS::sprintf (bp, "signal %d", sig);
#endif /* ACE_HAS_SYS_SIGLIST */
		    break;
		  }
		case 'T': // Format the timestamp.
		  {
		    type = SKIP_SPRINTF;
		    char day_and_time[35];
		    ACE_OS::sprintf (bp, "%s", 
				     ACE::timestamp (day_and_time, 
						     sizeof day_and_time));
		    break;		  
		  }
		case 't': // Format thread id. 
		  type = SKIP_SPRINTF;
#if defined (ACE_WIN32)
		  ACE_OS::sprintf (bp, "%u", ACE_Thread::self ());
#else
		  ACE_hthread_t t_id;
		  ACE_Thread::self (t_id);
		  ACE_OS::sprintf (bp, "%u", t_id);
#endif /* ACE_WIN32 */
		  break;
		case 's':
		  type = 1 + wpc; // 1, 2, 3 
		  break;
		case 'W':
		  // @@ UNICODE
		case 'd': case 'c': case 'i': case 'o': 
		case 'u': case 'x': case 'X': 
		  type = 4 + wpc; // 4, 5, 6 
		  break;
		case 'F': case 'f': case 'e': case 'E':
		case 'g': case 'G':
		  type = 7 + wpc; // 7, 8, 9 
		  break;
		case '*':	// consume width/precision 
		  w[wpc++] = va_arg (argp, int);
		  break;
		default:
		  // ? 
		  break;
		}
	    }

	  if (type != SKIP_SPRINTF)
	    {
	      c = *format;	// Remember char before we overwrite. 
	      *format = 0;	// Overwrite, terminating format. 

	      switch (type)
		{
		case 1:
		  ACE_OS::sprintf (bp, fp, va_arg (argp, char *));
		  break;
		case 2:
		  ACE_OS::sprintf (bp, fp, w[0], va_arg (argp, char *));
		  bp += w[0];
		  type = SKIP_NUL_LOCATE;
		  break;
		case 3:
		  ACE_OS::sprintf (bp, fp, w[0], w[1], va_arg (argp, char *));
		  bp += w[0];
		  type = SKIP_NUL_LOCATE;
		  break;
		case 4:
		  ACE_OS::sprintf (bp, fp, va_arg (argp, int));
		  break;
		case 5:
		  ACE_OS::sprintf (bp, fp, w[0], va_arg (argp, int));
		  break;
		case 6:
		  ACE_OS::sprintf (bp, fp, w[0], w[1], va_arg (argp, int));
		  break;
		case 7:
		  ACE_OS::sprintf (bp, fp, va_arg (argp, double));
		  break;
		case 8:
		  ACE_OS::sprintf (bp, fp, w[0], va_arg (argp, double));
		  break;
		case 9:
		  ACE_OS::sprintf (bp, fp, w[0], w[1], va_arg (argp, double));
		  break;
		}
	      *format = c;	// Restore char we overwrote. 
	    }

	  if (type != SKIP_NUL_LOCATE)
	    while (*bp != '\0')	// Locate end of bp. 
	      bp++;
	}
    }

  *bp = '\0'; // Terminate bp, but don't auto-increment this!

  ACE_OS::free (ACE_MALLOC_T (save_p));

  // Only print the message if "SILENT" mode is disabled.
  if (ACE_BIT_DISABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::SILENT))
    {
      // Copy the message from thread-specific storage into the
      // transfer buffer (this can be optimized away by changing other
      // code...).
      log_record.msg_data (this->msg ());
      this->stop_tracing ();

#if !defined (ACE_WIN32)
      // Make this block signal safe.
      ACE_Sig_Guard sb;
#endif /* ACE_WIN32 */

      // Make sure that the lock is held during all this.
      ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ace_mon, *ACE_Log_Msg_Manager::get_lock (), -1));

      if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::STDERR) 
	  && abort_prog == 0) // We'll get this further down.
	log_record.print (ACE_Log_Msg::local_host_,
			  ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::VERBOSE),
			  stderr);
      if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::LOGGER))
	{
	  ACE_Str_Buf log_msg ((void *) &log_record, 
			       int (log_record.length ()));
#if defined (ACE_HAS_STREAM_PIPES)
	  result = message_queue_.send (int (log_record.type ()), 
					&log_msg);
#elif !defined (ACE_WIN32)
	  result = message_queue_.send (log_msg);
#else
	  result = message_queue_.send ((const ACE_Str_Buf *) &log_msg, 
					(const ACE_Str_Buf *) 0);
#endif /* ACE_HAS_STREAM_PIPES */
	}
      // Format the message and print it to stderr and/or ship it
      // off to the log_client daemon, and/or print it to the
      // ostream.  This must come last, after the other two print
      // operations (see the ACE_Log_Record::print method for
      // details).

      if (ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::OSTREAM)
	  && this->msg_ostream () != 0)
	log_record.print (ACE_Log_Msg::local_host_,
			  ACE_BIT_ENABLED (ACE_Log_Msg::flags_, ACE_Log_Msg::VERBOSE),
			  *this->msg_ostream ());
      this->start_tracing ();
    }

  if (abort_prog)
    {
      // *Always* print a message to stderr if we're aborting.  We
      // don't use verbose, however, to avoid recursive aborts if
      // something is hosed.
      log_record.print (ACE_Log_Msg::local_host_, 0);
      ACE_OS::exit (exit_value);
    }

  return result;
}

// Calls log to do the actual print, but formats first.

int 
ACE_Log_Msg::log_hexdump (ACE_Log_Priority log_priority, 
			  const char *buffer, 
			  int size,
			  const char *text)
{
  char buf[ACE_Log_Record::MAXLOGMSGLEN - ACE_Log_Record::VERBOSE_LEN - 58];
  // 58 for the HEXDUMP header;

  char msg_buf[80];

  buf[0] = 0; // in case size = 0

  int len = ACE::format_hexdump (buffer, size, buf, sizeof buf);

  int sz = 0;

  if (text)
    sz = ACE_OS::sprintf (msg_buf, "%s - ", text);

  sz += ACE_OS::sprintf (msg_buf + sz, "HEXDUMP %d bytes", size);

  if (len < size)
    ACE_OS::sprintf (msg_buf + sz, " (showing first %d bytes)", len);

  // Now print out the formatted buffer.
  this->log (log_priority, "%s\n%s", msg_buf, buf);
  return 0;
}

void
ACE_Log_Msg::set (const char *filename, 
		  int line,
		  int status, 
		  int err, 
		  int rs,
		  ostream *os)
{
  ACE_TRACE ("ACE_Log_Msg::set");
  this->file (filename);
  this->linenum (line);
  this->op_status (status);
  this->errnum (err);
  this->restart (rs);
  this->msg_ostream (os);
}

void
ACE_Log_Msg::dump (void) const
{
  ACE_TRACE ("ACE_Log_Msg::dump");

  ACE_DEBUG ((LM_DEBUG, ACE_BEGIN_DUMP, this));
  ACE_DEBUG ((LM_DEBUG, "status_ = %d\n", this->status_));
  ACE_DEBUG ((LM_DEBUG, "\nerrnum_ = %d\n", this->errnum_));
  ACE_DEBUG ((LM_DEBUG, "\nlinenum_ = %d\n", this->linenum_));
  ACE_DEBUG ((LM_DEBUG, "\nfile_ = %s\n", this->file_));
  ACE_DEBUG ((LM_DEBUG, "\nmsg_ = %s\n", this->msg_));
  ACE_DEBUG ((LM_DEBUG, "\nrestart_ = %d\n", this->restart_));
  ACE_DEBUG ((LM_DEBUG, "\nostream_ = %x\n", this->ostream_));
  ACE_DEBUG ((LM_DEBUG, "\nprogram_name_ = %s\n", this->program_name_ ? this->program_name_ : "<unknown>"));
  ACE_DEBUG ((LM_DEBUG, "\nlocal_host_ = %s\n", this->local_host_ ? this->local_host_ : "<unknown>"));
  ACE_DEBUG ((LM_DEBUG, "\npid_ = %d\n", this->getpid ()));
  ACE_DEBUG ((LM_DEBUG, "\nflags_ = %x\n", this->flags_));
  ACE_DEBUG ((LM_DEBUG, "\ntrace_depth_ = %d\n", this->trace_depth_));
  ACE_DEBUG ((LM_DEBUG, "\trace_active_ = %d\n", this->trace_active_));
  ACE_DEBUG ((LM_DEBUG, "\tracing_enabled_ = %d\n", this->tracing_enabled_));
  ACE_DEBUG ((LM_DEBUG, "\npriority_mask_ = %d\n", this->priority_mask_));
  if (this->thr_state_ != 0)
    ACE_DEBUG ((LM_DEBUG, "\thr_state_ = %d\n", *this->thr_state_));
  ACE_DEBUG ((LM_DEBUG, "\nmsg_off_ = %d\n", this->msg_off_));
  message_queue_.dump ();
#if defined (ACE_MT_SAFE)  
  ACE_Log_Msg_Manager::get_lock ()->dump ();
  // Synchronize output operations.  
#endif /* ACE_MT_SAFE */
  ACE_DEBUG ((LM_DEBUG, ACE_END_DUMP));
}

void 
ACE_Log_Msg::op_status (int status)
{
  this->status_ = status;
}

int 
ACE_Log_Msg::op_status (void)
{
  return this->status_;
}

void 
ACE_Log_Msg::restart (int r)
{
  this->restart_ = r;
}

int 
ACE_Log_Msg::restart (void)
{
  return this->restart_;
}

int 
ACE_Log_Msg::errnum (void)
{
  return this->errnum_;
}

void
ACE_Log_Msg::errnum (int e)
{
  this->errnum_ = e;
}

int 
ACE_Log_Msg::linenum (void)
{
  return this->linenum_;
}

void
ACE_Log_Msg::linenum (int l)
{
  this->linenum_ = l;
}

int 
ACE_Log_Msg::inc (void) 
{ 
  return this->trace_depth_++; 
}

int 
ACE_Log_Msg::dec (void) 
{ 
  return --this->trace_depth_; 
}

int 
ACE_Log_Msg::trace_active (void) 
{ 
  return this->trace_active_; 
}

void 
ACE_Log_Msg::trace_active (int value) 
{ 
  this->trace_active_ = value; 
}

ACE_Thread_State *
ACE_Log_Msg::thr_state (void) 
{ 
  return this->thr_state_; 
}

void 
ACE_Log_Msg::thr_state (ACE_Thread_State *ts)
{ 
  this->thr_state_ = ts; 
}

ACE_hthread_t *
ACE_Log_Msg::thr_handle (void) 
{ 
  return this->thr_handle_; 
}

void 
ACE_Log_Msg::thr_handle (ACE_hthread_t *th)
{ 
  this->thr_handle_ = th; 
}

// Enable the tracing facility on a per-thread basis.

void 
ACE_Log_Msg::start_tracing (void) 
{ 
  this->tracing_enabled_ = 1;
}

// Disable the tracing facility on a per-thread basis.

void 
ACE_Log_Msg::stop_tracing (void)  
{ 
  this->tracing_enabled_ = 0; 
}

int
ACE_Log_Msg::tracing_enabled (void)
{ 
  return this->tracing_enabled_;
}

const char *
ACE_Log_Msg::file (void)
{
  return this->file_;
}

void
ACE_Log_Msg::file (const char *s)
{
  ACE_OS::strncpy (this->file_, s, 
		   (sizeof this->file_ / sizeof (char)));
}

char *
ACE_Log_Msg::msg (void)
{
  return this->msg_ + ACE_Log_Msg::msg_off_;
}

void
ACE_Log_Msg::msg (char *m)
{
  ACE_OS::strncpy (this->msg_, m, 
		   (sizeof this->msg_ / sizeof (char)));
}

ostream *
ACE_Log_Msg::msg_ostream (void)
{
  return this->ostream_;
}

void
ACE_Log_Msg::msg_ostream (ostream *m)
{
  this->ostream_ = m;
}

void
ACE_Log_Msg::local_host (const char *s)
{
#if defined (VXWORKS)
  // To avoid memory leak, don't copy the input string.  It had better
  // be in the text segment!
  ACE_Log_Msg::local_host_ = s;
#else
  if (s)
    {
      ACE_OS::free ((void *) ACE_Log_Msg::local_host_);
	  {
		  ACE_NO_HEAP_CHECK;

	      ACE_Log_Msg::local_host_ = ACE_OS::strdup (s);
	  }
    }
#endif /* VXWORKS */
}

const char *
ACE_Log_Msg::local_host (void) const
{
  return ACE_Log_Msg::local_host_;
}

pid_t
ACE_Log_Msg::getpid (void) const
{
  if (ACE_Log_Msg::pid_ == -1)
    ACE_Log_Msg::pid_ = ACE_OS::getpid ();

  return ACE_Log_Msg::pid_;
}


#if defined (ACE_TEMPLATES_REQUIRE_SPECIALIZATION)
template class ACE_Set_Node<ACE_Log_Msg *>;
template class ACE_Unbounded_Set<ACE_Log_Msg *>;
template class ACE_Unbounded_Set_Iterator<ACE_Log_Msg *>;
#endif /* ACE_TEMPLATES_REQUIRE_SPECIALIZATION */