FrontISTR  5.7.0
Large-scale structural analysis program with finit element method
hecmw_util.c
Go to the documentation of this file.
1 /*****************************************************************************
2  * Copyright (c) 2019 FrontISTR Commons
3  * This software is released under the MIT License, see LICENSE.txt
4  *****************************************************************************/
5 
6 #include <stdio.h>
7 #include <string.h>
8 #include <stdarg.h>
9 #include <time.h>
10 #include <ctype.h>
11 #include <errno.h>
12 #include "hecmw_config.h"
13 #include "hecmw_util.h"
14 
15 void HECMW_fprintf(FILE *fp, char *fmt, ...) {
16  va_list ap;
17  va_start(ap, fmt);
18  vfprintf(fp, fmt, ap);
19  va_end(ap);
20 }
21 
22 void HECMW_printerr(char *fmt, ...) {
23  va_list ap;
24  va_start(ap, fmt);
25  vfprintf(stderr, fmt, ap);
26  va_end(ap);
27 }
28 
29 char *HECMW_get_date(void) {
30  int rc;
31  time_t now;
32  static char static_buf[100];
33 
34  if (time(&now) == (time_t)-1) return NULL;
35  rc = strftime(static_buf, sizeof(static_buf), "%b %d %H:%M:%S",
36  localtime(&now));
37  return rc ? static_buf : NULL;
38 }
39 
40 /* thread-save version of HECMW_get_date */
41 char *HECMW_get_date_r(char *buf, int len) {
42  int rc;
43  time_t now;
44  struct tm result;
45 
46  if (time(&now) == (time_t)-1) return NULL;
47 
48 #if defined(__WIN32__) || defined(__WIN64__)
49  /* localtime_r is not available on Windows */
50  #pragma omp critical
51  {
52  rc = strftime(buf, len, "%b %d %H:%M:%S", localtime(&now));
53  }
54 #else
55  if (localtime_r(&now, &result) == NULL) return NULL;
56  rc = strftime(buf, len, "%b %d %H:%M:%S", &result);
57 #endif
58 
59  return rc ? buf : NULL;
60 }
61 
62 void HECMW_assert_(int cond, char *cond_str, char *file, int line) {
63  if (!cond) {
64  fprintf(stderr, "%s:%d: Assertion `%s' failed.\n", file, line, cond_str);
65 #ifdef HECMW_SERIAL
66  abort();
67 #else
68  MPI_Abort(MPI_COMM_WORLD, HECMW_EXIT_ERROR);
69 #endif
70  }
71 }
72 
73 int HECMW_check_condition_(int cond, char *cond_str, int isabort, char *file,
74  int line) {
75  if (cond) return 0;
76 
77  if (isabort) {
78  fprintf(stderr, "%s:%d: Assertion `%s' failed.\n", file, line, cond_str);
79 #ifdef HECMW_SERIAL
80  abort();
81 #else
82  MPI_Abort(MPI_COMM_WORLD, HECMW_EXIT_ERROR);
83 #endif
84  }
85  return 1;
86 }
87 
88 void HECMW_abort(HECMW_Comm comm) {
89 #ifdef HECMW_SERIAL
90  abort();
91 #else
92  /* HECMW_comm_is_initialized() ? MPI_Abort(comm, HECMW_EXIT_ERROR) :
93  * abort(); */
95  MPI_Abort(comm, HECMW_EXIT_ERROR);
96  } else {
97  abort();
98  }
99 #endif
100 }
101 
102 char *HECMW_toupper(char *s) {
103  char *p;
104 
105  if (s == NULL) return NULL;
106 
107  for (p = s; *p; p++) {
108  *p = (char)toupper(*p);
109  }
110  return s;
111 }
112 
113 char *HECMW_tolower(char *s) {
114  char *p;
115 
116  if (s == NULL) return NULL;
117 
118  for (p = s; *p; p++) {
119  *p = (char)tolower(*p);
120  }
121  return s;
122 }
123 
125 
126 void HECMW_print_vmsg(int loglv, int msgno, const char *fmt, va_list ap) {
127  char msg[HECMW_MSG_LEN + 1];
128  char vmsg[HECMW_MSG_LEN + 1];
129 
130  HECMW_snprintf(msg, sizeof(msg), "%s", HECMW_strmsg(msgno));
131  HECMW_vsnprintf(vmsg, sizeof(vmsg), fmt, ap);
132  if (strlen(vmsg) > 0) {
133  HECMW_snprintf(msg + strlen(msg), sizeof(msg) - strlen(msg), " (%s)", vmsg);
134  }
135  HECMW_log(loglv, msg);
136 }
137 
138 void HECMW_print_msg(int loglv, int msgno, const char *fmt, ...) {
139  va_list ap;
140  va_start(ap, fmt);
141  HECMW_print_vmsg(loglv, msgno, fmt, ap);
142  va_end(ap);
143 }
144 
145 int HECMW_vsnprintf(char *str, size_t size, const char *format, va_list ap) {
146 #ifdef WIN32_MSVC
147  return _vsnprintf(str, size, format, ap);
148 #else
149  return vsnprintf(str, size, format, ap);
150 #endif
151 }
152 
153 int HECMW_snprintf(char *str, size_t size, const char *format, ...) {
154  va_list ap;
155  int rtc;
156 
157  va_start(ap, format);
158 #ifdef WIN32_MSVC
159  rtc = _vsnprintf(str, size, format, ap);
160 #else
161  rtc = vsnprintf(str, size, format, ap);
162 #endif
163  va_end(ap);
164 
165  return rtc;
166 }
HECMW_check_condition_
int HECMW_check_condition_(int cond, char *cond_str, int isabort, char *file, int line)
Definition: hecmw_util.c:73
hecd_util::toupper
void toupper(char *s)
Definition: hecd_util.cpp:40
HECMW_snprintf
int HECMW_snprintf(char *str, size_t size, const char *format,...)
Definition: hecmw_util.c:153
HECMW_get_date
char * HECMW_get_date(void)
Definition: hecmw_util.c:29
HECMW_printerr
void HECMW_printerr(char *fmt,...)
Definition: hecmw_util.c:22
HECMW_log
int HECMW_log(int loglv, const char *fmt,...)
Definition: hecmw_log.c:260
HECMW_print_error
void HECMW_print_error(void)
Definition: hecmw_util.c:124
HECMW_print_vmsg
void HECMW_print_vmsg(int loglv, int msgno, const char *fmt, va_list ap)
Definition: hecmw_util.c:126
HECMW_get_date_r
char * HECMW_get_date_r(char *buf, int len)
Definition: hecmw_util.c:41
HECMW_tolower
char * HECMW_tolower(char *s)
Definition: hecmw_util.c:113
HECMW_comm_is_initialized
int HECMW_comm_is_initialized(void)
Definition: hecmw_comm.c:697
HECMW_EXIT_ERROR
#define HECMW_EXIT_ERROR
Definition: hecmw_config.h:62
HECMW_assert_
void HECMW_assert_(int cond, char *cond_str, char *file, int line)
Definition: hecmw_util.c:62
hecd_util::tolower
void tolower(char *s)
Definition: hecd_util.cpp:59
HECMW_vsnprintf
int HECMW_vsnprintf(char *str, size_t size, const char *format, va_list ap)
Definition: hecmw_util.c:145
HECMW_LOG_ERROR
#define HECMW_LOG_ERROR
Definition: hecmw_log.h:15
hecmw_config.h
HECMW_strmsg
char * HECMW_strmsg(int msgno)
Definition: hecmw_msg.c:31
HECMW_Comm
MPI_Comm HECMW_Comm
Definition: hecmw_config.h:30
HECMW_print_msg
void HECMW_print_msg(int loglv, int msgno, const char *fmt,...)
Definition: hecmw_util.c:138
HECMW_toupper
char * HECMW_toupper(char *s)
Definition: hecmw_util.c:102
NULL
#define NULL
Definition: hecmw_io_nastran.c:30
HECMW_get_errmsg
char * HECMW_get_errmsg(void)
Definition: hecmw_error.c:58
hecmw_util.h
HECMW_MSG_LEN
#define HECMW_MSG_LEN
Definition: hecmw_config.h:74
HECMW_abort
void HECMW_abort(HECMW_Comm comm)
Definition: hecmw_util.c:88
HECMW_fprintf
void HECMW_fprintf(FILE *fp, char *fmt,...)
Definition: hecmw_util.c:15