File: sge_log.c

package info (click to toggle)
gridengine 8.1.9%2Bdfsg-13.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 57,140 kB
  • sloc: ansic: 432,689; java: 87,068; cpp: 31,958; sh: 29,445; jsp: 7,757; perl: 6,336; xml: 5,828; makefile: 4,704; csh: 3,934; ruby: 2,221; tcl: 1,676; lisp: 669; yacc: 519; python: 503; lex: 361; javascript: 200
file content (895 lines) | stat: -rw-r--r-- 25,513 bytes parent folder | download | duplicates (6)
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
/*___INFO__MARK_BEGIN__*/
/*************************************************************************
 * 
 *  The Contents of this file are made available subject to the terms of
 *  the Sun Industry Standards Source License Version 1.2
 * 
 *  Sun Microsystems Inc., March, 2001
 * 
 * 
 *  Sun Industry Standards Source License Version 1.2
 *  =================================================
 *  The contents of this file are subject to the Sun Industry Standards
 *  Source License Version 1.2 (the "License"); You may not use this file
 *  except in compliance with the License. You may obtain a copy of the
 *  License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
 * 
 *  Software provided under this License is provided on an "AS IS" basis,
 *  WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
 *  WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
 *  MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
 *  See the License for the specific provisions governing your rights and
 *  obligations concerning the Software.
 * 
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 * 
 *   Copyright: 2001 by Sun Microsystems, Inc.
 * 
 *   All Rights Reserved.
 * 
 ************************************************************************/
/*___INFO__MARK_END__*/

#include <fcntl.h>
#include <string.h>
#include <stdio.h>
#include <pthread.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "uti/sge_rmon.h"
#include "uti/sge_log.h"
#include "uti/sge_time.h"
#include "uti/sge_dstring.h"
#include "uti/sge_prog.h"
#include "uti/sge_uidgid.h"
#include "uti/sge_mtutil.h"

#include "gdi/sge_gdi_ctx.h"
#include "uti/sge_string.h"

#include "uti/msg_utilib.h"

#include "sge.h"

typedef struct {
   pthread_mutex_t  mutex;
   char*            log_file;
   u_long32         log_level;
   int              log_as_admin_user;
   int              verbose;
   int              gui_log;
} log_state_t;

/* Fixme:  Any log message must fit into this.  What guarantee do we
   have that it can't overflow?  */
typedef struct {
   char  log_buffer[4 * MAX_STRING_SIZE]; /* a.k.a. SGE_EVENT */
} log_buffer_t;

typedef struct {
   sge_gdi_ctx_class_t *context;
} log_context_t;


static log_state_t Log_State = {PTHREAD_MUTEX_INITIALIZER, "syslog.init", LOG_WARNING, 0, 1, 1};

static pthread_once_t log_buffer_once = PTHREAD_ONCE_INIT;
static pthread_key_t  log_buffer_key;

static pthread_once_t log_context_once = PTHREAD_ONCE_INIT;
static pthread_key_t  log_context_key;

static void log_buffer_once_init(void);
static void log_context_once_init(void);

static void log_buffer_destroy(void* theState);
static log_buffer_t* log_buffer_getspecific(void);

static void log_context_destroy(void* theState);
static log_context_t* log_context_getspecific(void);
static sge_gdi_ctx_class_t* log_state_get_log_context(void);


static void sge_do_log(u_long32 prog_number,
                       const char *progname,
                       const char *unqualified_hostname,
                       int aLevel, 
                       const char* aMessage,
                       int log_level);

static sge_gdi_ctx_class_t *log_get_log_context(void);
static void log_set_log_context(sge_gdi_ctx_class_t *theCtx);

/****** uti/log/log_get_log_buffer() ******************************************
*  NAME
*     log_get_log_buffer() -- Return a buffer that can be used to build logging 
*                                   strings
*
*  SYNOPSIS
*     char *log_get_log_buffer(void) 
*
*  FUNCTION
*     Return a buffer that can be used to build logging strings
*
*  RESULT
*     char * 
*
******************************************************************************/
char *log_get_log_buffer(void)
{
   log_buffer_t *buf = NULL;
   char *log_buffer = NULL;

   buf = log_buffer_getspecific();

   if (buf != NULL) {
      log_buffer = buf->log_buffer;
   }

   return log_buffer;
}

/****** uti/log/log_get_log_context() ******************************************
*  NAME
*     log_get_log_context() -- Return a context for the specific thread 
*
*  SYNOPSIS
*     sge_gdi_ctx_class_t* log_get_log_context(void) 
*
*  FUNCTION
*     Return a context for the specific thread
*
*  RESULT
*     sge_gdi_ctx_class_t* 
*
******************************************************************************/
static sge_gdi_ctx_class_t* log_get_log_context(void)
{
   log_context_t *log_ctx = NULL;
   sge_gdi_ctx_class_t *context = NULL;

   log_ctx = log_context_getspecific();

   if (log_ctx != NULL) {
      context = log_ctx->context;
   }   

   return context;
}

/****** uti/log/log_set_log_context() ******************************************
*  NAME
*     log_set_log_context() -- set a context for the specific thread 
*
*  SYNOPSIS
*     void log_set_log_context(void *newctx) 
*
*  FUNCTION
*     Return a context for the specific thread
*
*  INPUTS
*     void newctx - the new ctx
*
*  RESULT
*
*  NOTES
*     MT-NOTE: log_state_set_log_file() is not MT safe.
*     MT-NOTE:
*     MT-NOTE: It is safe, however, to call this function from within multiple
*     MT-NOTE: threads as long as no other thread calls log_state_set_log_file() 
*
******************************************************************************/
static void log_set_log_context(sge_gdi_ctx_class_t *newctx)
{
   log_context_t *log_ctx = NULL;

   log_ctx = log_context_getspecific();

   if (log_ctx != NULL) {
      log_ctx->context = newctx;
   } 
}

/****** uti/log/log_state_get_log_level() ******************************************
*  NAME
*     log_state_get_log_level() -- Return log level.
*
*  SYNOPSIS
*     u_long32 log_state_get_log_level(void) 
*
*  FUNCTION
*     Return log level
*
*  RESULT
*     u_long32
*
******************************************************************************/
u_long32 log_state_get_log_level(void)
{
   u_long32 level = 0;

   sge_mutex_lock("Log_State_Lock", "log_state_get_log_level", __LINE__, &Log_State.mutex);

   level = Log_State.log_level;

   sge_mutex_unlock("Log_State_Lock", "log_state_get_log_level", __LINE__, &Log_State.mutex);

   return level;
}

/****** uti/sge_log/log_state_get_log_file() ***********************************
*  NAME
*     log_state_get_log_file() -- get log file name
*
*  SYNOPSIS
*     const char* log_state_get_log_file(void) 
*
*  FUNCTION
*     Return name of current log file. The string returned may or may not 
*     contain a path.
*
*  INPUTS
*     void - none 
*
*  RESULT
*     const char* - log file name (with relative or absolute path)
*
*  NOTES
*     MT-NOTE: log_state_get_log_file() is not MT safe.
*     MT-NOTE:
*     MT-NOTE: It is safe, however, to call this function from within multiple
*     MT-NOTE: threads as long as no other thread does change 'Log_File'.
*
*  BUGS
*     BUGBUG-AD: This function should use something like a barrier for
*     BUGBUG-AD: synchronization.
*
*******************************************************************************/
const char *log_state_get_log_file(void)
{
   char* file = NULL;

   sge_mutex_lock("Log_State_Lock", "log_state_get_log_file", __LINE__, &Log_State.mutex);

   file = Log_State.log_file;

   sge_mutex_unlock("Log_State_Lock", "log_state_get_log_file", __LINE__, &Log_State.mutex);

   return file;
}

/****** uti/log/log_state_get_log_verbose() ******************************************
*  NAME
*     log_state_get_log_verbose() -- Is verbose logging enabled?
*
*  SYNOPSIS
*     int log_state_get_log_verbose(void) 
*
*  FUNCTION
*     Is verbose logging enabled? 
*     With verbose logging enabled not only ERROR/CRITICAL messages are 
*     printed to stderr but also WARNING/INFO.
*
*  RESULT
*     int - 0 or 1 
*
*  SEE ALSO
*     uti/log/log_state_set_log_verbose()
******************************************************************************/
int log_state_get_log_verbose(void)
{
   int verbose = 0;

   sge_mutex_lock("Log_State_Lock", "log_state_get_log_verbose", __LINE__, &Log_State.mutex);
   
   verbose = Log_State.verbose;
   
   sge_mutex_unlock("Log_State_Lock", "log_state_get_log_verbose", __LINE__, &Log_State.mutex);

   return verbose;
}

/****** uti/log/log_state_get_log_gui() ******************************************
*  NAME
*     log_state_get_log_gui() -- Is GUI logging enabled?
*
*  SYNOPSIS
*     int log_state_get_log_gui(void) 
*
*  FUNCTION
*     Is GUI logging enabled? 
*     With GUI logging enabled messages are printed to stderr/stdout.  
*
*  RESULT
*     int - 0 or 1 
*
*  SEE ALSO
*     uti/log/log_state_set_log_gui()
******************************************************************************/
int log_state_get_log_gui(void)
{
   int gui_log = 0;

   sge_mutex_lock("Log_State_Lock", "log_state_get_log_gui", __LINE__, &Log_State.mutex);
   
   gui_log = Log_State.gui_log;

   sge_mutex_unlock("Log_State_Lock", "log_state_get_log_gui", __LINE__, &Log_State.mutex);

   return gui_log;
}

/****** uti/log/log_state_get_log_as_admin_user() ******************************************
*  NAME
*     log_state_get_log_as_admin_user() -- Needs sge_log() to change into admin user?
*
*  SYNOPSIS
*     trace_func_type log_state_get_log_as_admin_user(void) 
*
*  FUNCTION
*     Returns whether logging shall be done as admin user.
*
*  RESULT
*     int
*
*  SEE ALSO
*     uti/log/log_state_set_log_as_admin_user()
******************************************************************************/
int log_state_get_log_as_admin_user(void)
{
   int log_as_admin_user = 0;

   sge_mutex_lock("Log_State_Lock", "log_state_get_log_as_admin_user", __LINE__, &Log_State.mutex);
   
   log_as_admin_user = Log_State.log_as_admin_user;
   
   sge_mutex_unlock("Log_State_Lock", "log_state_get_log_as_admin_user", __LINE__, &Log_State.mutex);

   return log_as_admin_user;
}

/****** uti/log/log_state_set_log_level() *****************************************
*  NAME
*     log_state_set_log_level() -- Set log level to be used.
*
*  SYNOPSIS
*     void log_state_set_log_level(int i) 
*
*  FUNCTION
*     Set log level to be used.
*
*  INPUTS
*     u_long32 
*
*  SEE ALSO
*     uti/log/log_state_get_log_level() 
******************************************************************************/
void log_state_set_log_level(u_long32 theLevel)
{ 

   sge_mutex_lock("Log_State_Lock", "log_state_set_log_level", __LINE__, &Log_State.mutex);
   
   Log_State.log_level = theLevel;

   sge_mutex_unlock("Log_State_Lock", "log_state_set_log_level", __LINE__, &Log_State.mutex);

   return;
}

void log_state_set_log_file(char *theFile)
{
   sge_mutex_lock("Log_State_Lock", "log_state_set_log_file", __LINE__, &Log_State.mutex);

   /* Don't change it if it's been set to syslog in the execd or
      qmaster params.  */
   if (strcmp( Log_State.log_file, "syslog") != 0)
      Log_State.log_file = theFile;
   
   sge_mutex_unlock("Log_State_Lock", "log_state_set_log_file", __LINE__, &Log_State.mutex);

   return;
}

/****** uti/log/log_state_set_log_verbose() *****************************************
*  NAME
*     log_state_set_log_verbose() -- Enable/disable verbose logging 
*
*  SYNOPSIS
*     void log_state_set_log_verbose(int i) 
*
*  FUNCTION
*     Enable/disable verbose logging 
*
*  INPUTS
*     int i - 0 or 1  
*
*  SEE ALSO
*     uti/log/log_state_get_log_verbose() 
******************************************************************************/
void log_state_set_log_verbose(int i)
{
   sge_mutex_lock("Log_State_Lock", "log_state_set_log_verbose", __LINE__, &Log_State.mutex);
   
   Log_State.verbose = i;
   
   sge_mutex_unlock("Log_State_Lock", "log_state_set_log_verbose", __LINE__, &Log_State.mutex);

   return;
}

/****** uti/log/log_state_set_log_gui() ********************************************
*  NAME
*     log_state_set_log_gui() -- Enable/disable logging for GUIs 
*
*  SYNOPSIS
*     void log_state_set_log_gui(int i) 
*
*  FUNCTION
*     Enable/disable logging for GUIs 
*     With GUI logging enabled messages are printed to stderr/stdout.  
*
*  INPUTS
*     int i - 0 or 1 
******************************************************************************/
void log_state_set_log_gui(int i)
{

   sge_mutex_lock("Log_State_Lock", "log_state_set_log_gui", __LINE__, &Log_State.mutex);
   
   Log_State.gui_log = i;

   sge_mutex_unlock("Log_State_Lock", "log_state_set_log_gui", __LINE__, &Log_State.mutex);
   
   return;
}   

/****** uti/log/log_state_set_log_as_admin_user() *****************************
*  NAME
*     log_state_set_log_as_admin_user() -- Enable/Disable logging as admin user 
*
*  SYNOPSIS
*     void log_state_set_log_as_admin_user(int i)
*
*  FUNCTION
*     This function enables/disables logging as admin user. This 
*     means that the function/macros switches from start user to 
*     admin user before any messages will be written. After that 
*     they will switch back to 'start' user. 
*
*  INPUTS
*     int i - 0 or 1 
*  
*  SEE ALSO
*     uti/uidgid/sge_switch2admin_user
*     uti/uidgid/sge_switch2start_user
******************************************************************************/
void log_state_set_log_as_admin_user(int i)
{
   sge_mutex_lock("Log_State_Lock", "log_state_set_log_as_admin_user", __LINE__, &Log_State.mutex);
   
   Log_State.log_as_admin_user = i;

   sge_mutex_unlock("Log_State_Lock", "log_state_set_log_as_admin_user", __LINE__, &Log_State.mutex);
}


static sge_gdi_ctx_class_t* log_state_get_log_context(void)
{
   sge_gdi_ctx_class_t *log_context = NULL;

/*    sge_mutex_lock("Log_State_Lock", "log_state_get_log_context", __LINE__, &Log_State.mutex); */

   log_context = log_get_log_context();

/*    sge_mutex_unlock("Log_State_Lock", "log_state_get_log_context", __LINE__, &Log_State.mutex); */

   return log_context;
}

void log_state_set_log_context(void *theCtx)
{
   sge_mutex_lock("Log_State_Lock", "log_state_set_log_context", __LINE__, &Log_State.mutex);
   
   log_set_log_context((sge_gdi_ctx_class_t*)theCtx);

   sge_mutex_unlock("Log_State_Lock", "log_state_set_log_context", __LINE__, &Log_State.mutex);
}



/****** uti/log/sge_log() *****************************************************
*  NAME
*     sge_log() -- Low level logging function 
*
*  SYNOPSIS
*     int sge_log(int log_level, char *mesg, char *file__, 
*                 char *func__, int line__) 
*
*  FUNCTION
*     Low level logging function. Used by various macros.
*     Do not use this function directly. 
*
*  INPUTS
*     int log_level - Logging Level
*     char *mesg    - Message
*     char *file__  - Filename
*     char *func__  - Function Name
*     int line__    - Line within 'file__'
*
*  RESULT
*     int - 0
*
*  SEE ALSO
*     uti/log/CRITICAL
*     uti/log/ERROR
*     uti/log/WARNING
*     uti/log/NOTICE
*     uti/log/INFO
*     uti/log/DEBUG
*
*  NOTES
*     MT-NOTE: sge_log() is not MT safe due to sge_switch2admin_user()
*     MT-NOTE: sge_log() can be used in clients where no admin user switching 
*     MT-NOTE: takes place
*     MT-NOTE: sge_log() is not MT safe due rmon_condition()
*     MT-NOTE: sge_log() can be used if DENTER_MAIN() is called only by one 
*     MT-NOTE: thread
******************************************************************************/
int sge_log(int log_level, const char *mesg, const char *file__, const char *func__, int line__) 
{
   char buf[128*4];
   int levelchar;
   char levelstring[32*4];
   
   sge_gdi_ctx_class_t *ctx = NULL;
   /* TODO: this must be kept for qmaster and should be done in a different
            way (qmaster context) !!! */
   u_long32 me = 0;
   const char *threadname = NULL;
   const char *unqualified_hostname = NULL;
   int is_daemonized = 0; 

   DENTER_(BASIS_LAYER, "sge_log");
   
   ctx = log_state_get_log_context();
   
   if (ctx != NULL) {
      me = ctx->get_who(ctx);
      threadname = ctx->get_thread_name(ctx);
      unqualified_hostname = ctx->get_unqualified_hostname(ctx);
      is_daemonized = ctx->is_daemonized(ctx);
   } else {
      DPRINTF(("sge_log: ctx is NULL\n"));
   }   

   /* Make sure to have at least a one byte logging string */
   if (!mesg || mesg[0] == '\0') {
      snprintf(buf, sizeof(buf), MSG_LOG_CALLEDLOGGINGSTRING_S,
               mesg ? MSG_LOG_ZEROLENGTH : MSG_POINTER_NULL);
      mesg = buf;
   }

   DPRINTF(("%s %d %s\n", file__, line__, mesg));

   /* quick exit if nothing to log */
   if (log_level > MAX(log_state_get_log_level(), LOG_WARNING)) {
      DRETURN_(0);
   }

   if (!log_state_get_log_gui()) {
      DRETURN_(0);
   }

   switch(log_level) {
      case LOG_PROF:
         sge_strlcpy(levelstring, MSG_LOG_PROFILING, sizeof(levelstring));
         levelchar = 'P';
         break;
      case LOG_CRIT:
         sge_strlcpy(levelstring, MSG_LOG_CRITICALERROR, sizeof(levelstring));
         levelchar = 'C';
         break;
      case LOG_ERR:
         sge_strlcpy(levelstring, MSG_LOG_ERROR, sizeof(levelstring));
         levelchar = 'E';
         break;
      case LOG_WARNING:
         strcpy(levelstring, "");
         levelchar = 'W';
         break;
      case LOG_NOTICE:
         strcpy(levelstring, "");
         levelchar = 'N';
         break;
      case LOG_INFO:
         strcpy(levelstring, "");
         levelchar = 'I';
         break;
      case LOG_DEBUG:
         strcpy(levelstring, "");
         levelchar = 'D';
         break;
      default:
         strcpy(levelstring, "");
         levelchar = 'L';
         log_level = LOG_INFO;
         break;
   }

   /* avoid double output in debug mode */
   if (!is_daemonized && !rmon_condition(TOP_LAYER, INFOPRINT) && 
       (log_state_get_log_verbose() || log_level <= LOG_WARNING)) {
      fprintf(stderr, "%s%s\n", levelstring, mesg);
   }

   sge_do_log(me, threadname, unqualified_hostname, levelchar, mesg, log_level);

   DRETURN_(0);
} /* sge_log() */

/****** uti/sge_log/sge_do_log() ***********************************************
*  NAME
*     sge_do_log() -- Write message to log file (or syslog)
*
*  SYNOPSIS
*     static void sge_do_log(u_long32 me, const char* progname, const char* unqualified_hostname, int aLevel, const char *aMessage, int log_level)
*
*  FUNCTION
*     ??? 
*
*  INPUTS
*     u_long32 me          - process type (QMASTER etc.)
*     const char* progname - program name
*     const char* unqualified_hostname - name of host issuing message
*     int aLevel           - log level
*     const char *aMessage - log message
*     int log_level        - numeric level from which aLevel is derived
*
*  RESULT
*     void - none
*
*  NOTES
*     MT-NOTE: sge_do_log() is MT safe.
*
*******************************************************************************/
static void sge_do_log(u_long32 me, const char* progname, const char* unqualified_hostname,
                       int aLevel, const char *aMessage, int log_level)
{
   int fd;

   if (me == QMASTER || me == EXECD || me == SCHEDD || me == SHADOWD) {
      const char *logfile = log_state_get_log_file();
      char msg2log[4*MAX_STRING_SIZE];
      dstring msg;

      sge_dstring_init(&msg, msg2log, sizeof(msg2log));

      if (strncmp(logfile, "syslog", 6) == 0) {
         if (LOG_PROF == log_level)
            log_level = LOG_INFO;
         sge_dstring_sprintf_append(&msg, "%6.6s|%s|%c|%s\n",
                                    progname, unqualified_hostname,
                                    aLevel, aMessage);
         syslog(log_level | LOG_USER, "%s", msg2log);
      } else if ((fd = SGE_OPEN3(logfile, O_WRONLY | O_APPEND | O_CREAT,
                                 0666)) >= 0) {
         int len;

         append_time((time_t)sge_get_gmt(), &msg, false);

         sge_dstring_sprintf_append(&msg, "|%6.6s|%s|%c|%s\n",
                 progname,
                 unqualified_hostname,
                 aLevel,
                 aMessage);

         len = strlen(msg2log);
         if (write(fd, msg2log, len) != len) {
            /* we are in error logging here - the only chance to log this problem
             * might be to write it to stderr
             */
            fprintf(stderr, "can't log to file %s: %s\n", log_state_get_log_file(), sge_strerror(errno, &msg));
         }
         close(fd);
      }
   }

   return;
} /* sge_do_log() */

/****** uti/log/log_buffer_once_init() ********************************************
*  NAME
*     log_buffer_once_init() -- One-time logging initialization.
*
*  SYNOPSIS
*     static log_buffer_once_init(void) 
*
*  FUNCTION
*     Create access key for thread local storage. Register cleanup function.
*     This function must be called exactly once.
*
*  INPUTS
*     void - none
*
*  RESULT
*     void - none 
*
*  NOTES
*     MT-NOTE: log_buffer_once_init() is MT safe. 
*
*******************************************************************************/
static void log_buffer_once_init(void)
{
   pthread_key_create(&log_buffer_key, &log_buffer_destroy);
} /* log_once_init */

/****** uti/log/log_buffer_destroy() ****************************************
*  NAME
*     log_buffer_destroy() -- Free thread local storage
*
*  SYNOPSIS
*     static void log_buffer_destroy(void* theState) 
*
*  FUNCTION
*     Free thread local storage.
*
*  INPUTS
*     void* theState - Pointer to memroy which should be freed.
*
*  RESULT
*     static void - none
*
*  NOTES
*     MT-NOTE: log_buffer_destroy() is MT safe.
*
*******************************************************************************/
static void log_buffer_destroy(void* theBuffer)
{
   sge_free(&theBuffer);
}

/****** uti/log/log_buffer_getspecific() ****************************************
*  NAME
*     log_buffer_getspecific() -- Get thread local log state
*
*  SYNOPSIS
*     static log_buffer_t* log_buffer_getspecific() 
*
*  FUNCTION
*     Return thread local log state.
*
*     If a given thread does call this function for the first time, no thread
*     local log state is available for this particular thread. In this case the
*     thread local log state is allocated and set.
*
*  RESULT
*     static log_buffer_t* - Pointer to thread local log state.
*
*  NOTES
*     MT-NOTE: log_buffer_getspecific() is MT safe 
*
*******************************************************************************/
static log_buffer_t* log_buffer_getspecific(void)
{
   log_buffer_t *buf = NULL;
   int res = -1;

   pthread_once(&log_buffer_once, log_buffer_once_init);

   if ((buf = pthread_getspecific(log_buffer_key)) != NULL) {
      return buf;
   }

   buf = (log_buffer_t*)sge_malloc(sizeof(log_buffer_t));
   memset((void*)(buf), 0, sizeof(log_buffer_t));

   res = pthread_setspecific(log_buffer_key, (const void*)buf);

   if (0 != res) {
      fprintf(stderr, "pthread_set_specific(%s) failed: %s\n", "log_buffer_getspecific", strerror(res));
      abort();
   }
   
   return buf;
} /* log_buffer_getspecific() */


/****** uti/log/log_context_once_init() ********************************************
*  NAME
*     log_context_once_init() -- One-time logging initialization.
*
*  SYNOPSIS
*     static log_context_once_init(void) 
*
*  FUNCTION
*     Create access key for thread local storage. Register cleanup function.
*     This function must be called exactly once.
*
*  INPUTS
*     void - none
*
*  RESULT
*     void - none 
*
*  NOTES
*     MT-NOTE: log_context_once_init() is MT safe. 
*
*******************************************************************************/
static void log_context_once_init(void)
{
   pthread_key_create(&log_context_key, &log_context_destroy);
} /* log_once_init */

/****** uti/log/log_context_destroy() ****************************************
*  NAME
*     log_context_destroy() -- Free thread local storage
*
*  SYNOPSIS
*     static void log_context_destroy(void* theState) 
*
*  FUNCTION
*     Free thread local storage.
*
*  INPUTS
*     void* theState - Pointer to memroy which should be freed.
*
*  RESULT
*     static void - none
*
*  NOTES
*     MT-NOTE: log_context_destroy() is MT safe.
*
*******************************************************************************/
static void log_context_destroy(void* theContext)
{
   sge_free(&theContext);
}

/****** uti/log/log_context_getspecific() ****************************************
*  NAME
*     log_context_getspecific() -- Get thread local log context
*
*  SYNOPSIS
*     static log_context_t* log_context_getspecific() 
*
*  FUNCTION
*     Return thread local log context.
*
*     If a given thread does call this function for the first time, no thread
*     local log state is available for this particular thread. In this case the
*     thread local log state is allocated and set.
*
*  RESULT
*     static log_context_t* - Pointer to thread local log context.
*
*  NOTES
*     MT-NOTE: log_context_getspecific() is MT safe 
*
*******************************************************************************/
static log_context_t* log_context_getspecific(void)
{
   log_context_t *myctx = NULL;
   int res = -1;

   pthread_once(&log_context_once, log_context_once_init);

   if ((myctx = pthread_getspecific(log_context_key)) != NULL) {
      return myctx;
   }

   myctx = (log_context_t*)sge_malloc(sizeof(log_context_t));
   if (myctx != NULL) {
      myctx->context = NULL;
   }   
   res = pthread_setspecific(log_context_key, (const void*)myctx);

   if (0 != res) {
      fprintf(stderr, "pthread_set_specific(%s) failed: %s\n", "log_context_getspecific", strerror(res));
      abort();
   }
   
   return myctx;
} /* log_context_getspecific() */