File: exx_log.c

package info (click to toggle)
openmx 3.7.6-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 325,856 kB
  • ctags: 3,575
  • sloc: ansic: 152,655; f90: 2,080; python: 876; makefile: 675; sh: 25; perl: 18
file content (221 lines) | stat: -rw-r--r-- 4,008 bytes parent folder | download | duplicates (2)
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
/*----------------------------------------------------------------------
  exx_log.c

  simple logging tool

  Coded by M. Toyoda, 16 Dec. 2009 
----------------------------------------------------------------------*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdarg.h>
#include <assert.h>
#include "exx.h"
#include "exx_log.h"

#if (defined _BSD_SOURCE || _XOPEN_SOURCE >= 500)
#include <unistd.h>
#endif

#ifndef HOST_NAME_MAX
#define HOST_NAME_MAX 255
#endif 


static void my_gethostname(char *name, size_t length)
{
  char *env;

  /* name should be longer than HOST_NAME_MAX */
#if (defined _BSD_SOURCE || _XOPEN_SOURCE >= 500)
  gethostname(name, length);
  return;
#endif

  /* check env list */
  env = getenv("HOSTNAME");
  if (env && strlen(env)) {
    strncpy(name, env, length);
    return;
  }
  
  /* no way to know it */
  strncpy(name, "host-name-unknown", length);
}



void EXX_Log_StdOut(const char *fmt, ... )
{
  int myrank;
  va_list argp;

#ifdef EXX_USE_MPI
  MPI_Comm_rank(g_exx_mpicomm, &myrank);
#else
  myrank = EXX_ROOT_RANK;
#endif /* EXX_USE_MPI */

  if (EXX_ROOT_RANK == myrank) {
    va_start( argp, fmt );
    vfprintf(stdout, fmt, argp );
    va_end( argp ); 
    fflush( stdout );
  }
}


void EXX_Log_StdErr(const char *fmt, ... )
{
  int myrank;
  va_list argp;

#ifdef EXX_USE_MPI
  MPI_Comm_rank(g_exx_mpicomm, &myrank);
#else
  myrank = EXX_ROOT_RANK;
#endif /* EXX_USE_MPI */

  if (EXX_ROOT_RANK == myrank) {
    va_start( argp, fmt );
    vfprintf(stderr, fmt, argp );
    va_end( argp ); 
    fflush( stderr );
  }
}


void EXX_Error(const char *msg, const char *file, int line)
{
  fprintf(stderr, "***Error in %s (%d)\n", file, line);
  fprintf(stderr, "   %s\n", msg);

#ifdef EXX_USE_MPI
  MPI_Abort(g_exx_mpicomm, -1);
#else
  abort();
#endif
}


void EXX_Warn(const char *msg, const char *file, int line)
{
  fprintf(stderr, "***Warning in %s (%d)\n", file, line);
  fprintf(stderr, "   %s\n", msg);
}


 
#if EXX_LOG_SWITCH

static FILE* fp_log;


void EXX_Log_Open(void)
{
  int nproc, myrank;
  char path[EXX_PATHLEN], host[HOST_NAME_MAX];
  time_t loctim;

  assert( NULL == fp_log );

#ifdef EXX_USE_MPI
  MPI_Comm_rank(g_exx_mpicomm, &myrank);
  MPI_Comm_size(g_exx_mpicomm, &nproc);
#else
  myrank = EXX_ROOT_RANK;
  nproc = 1;
#endif /* EXX_USE_MPI */ 

  /* filename */
  sprintf(path, "log%03d.txt", myrank);
  
  /* open */
  fp_log = fopen(path, "wt");
 
  /* header message */
  fprintf(fp_log, "EXX-LOG OPEN\n");

  /* current date and time */
  time(&loctim);
  fprintf(fp_log, "  DATETIME= %s", ctime(&loctim));

  /* mpi */
  fprintf(fp_log, "  MPI_RANK= %d\n", myrank);
  fprintf(fp_log, "  MPI_NPROC= %d\n", nproc);

  /* host name */
  my_gethostname(host, HOST_NAME_MAX);
  fprintf(fp_log, "  HOSTNAME= %s\n", host);

  /* otuput imediately (if possible) */
  fflush(fp_log);
}


void EXX_Log_Close(void)
{
  time_t loctim;

  /* show tail message */
  fprintf(fp_log, "EXX-LOG CLOSING\n");

  /* current date and time */
  time(&loctim);
  fprintf(fp_log, "  DATETIME= %s", ctime(&loctim));

  fprintf(fp_log, "  GOOD-BYE.\n");

  /* close file */
  fclose(fp_log);

  /* reset the pointer */
  fp_log = NULL;
}


void EXX_Log_Print(const char *fmt, ... )
{
  va_list argp;

  va_start( argp, fmt );
  vfprintf(fp_log, fmt, argp );
  va_end( argp ); 

  fflush(fp_log);
}


void EXX_Log_Message(const char *message)
{
  EXX_Log_Print("%s\n", message);
}


void EXX_Log_Trace_Integer(const char *key, int val)
{
  EXX_Log_Print("TRACE: %15s= %10d\n", key, val);
}


void EXX_Log_Trace_Double(const char *key, double val)
{
  EXX_Log_Print("TRACE: %15s= %20.10f\n", key, val);
}


void EXX_Log_Trace_String(const char *key, const char *val)
{
  EXX_Log_Print("TRACE: %15s= \"%s\"\n", key, val);
}


void EXX_Log_Trace_Vector(const char *key, const double *val)
{
  EXX_Log_Print("TRACE %15s= ( %12.6f, %12.6f, %12.6f )\n", 
    key, val[0], val[1], val[2]);
}

#endif /* EXX_LOG_SWITCH */