Flow123d  JS_before_hm-62-ge56f9d5
system.hh
Go to the documentation of this file.
1 /*!
2  *
3  * Copyright (C) 2015 Technical University of Liberec. All rights reserved.
4  *
5  * This program is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU General Public License version 3 as published by the
7  * Free Software Foundation. (http://www.gnu.org/licenses/gpl-3.0.en.html)
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
12  *
13  *
14  * @file system.hh
15  * @brief
16  */
17 
18 #ifndef SYSTEM_H
19 #define SYSTEM_H
20 
21 
22 #include <mpi.h>
23 //#include <iostream>
24 
25 #include "system/global_defs.h"
26 #include "system/exc_common.hh"
27 #include <stdio.h> // for FILE
28 #include <boost/exception/detail/error_info_impl.hpp> // for error_info
29 #include <boost/exception/info.hpp> // for operator<<
30 #include <string> // for string
31 #include "system/exceptions.hh" // for ExcChkErr::~Ex...
32 #include "system/fmt/posix.h" // for FMT_UNUSED
33 
34 
35 #ifndef _BSD_SOURCE
36  #define _BSD_SOURCE
37 #endif
38 
39 #ifdef WINDOWS
40  #include <direct.h>
41  #define GetCurrentDir _getcwd
42  #define PATH_MAX MAX_PATH
43 #else
44  //#include <unistd.h>
45  //#define GetCurrentDir getcwd
46  //#include <limits.h> //PATH_MAX
47 #endif
48 
49 #define strcmpi strcasecmp
50 #define DIR_DELIMITER '/'
51 
52 
53 // Assuming all compilers supports CXX11 features
54 #define OPERATOR_NEW_THROW_EXCEPTION
55 
56 
57 
58 using namespace std;
59 
60 
61 // **************************************************************
62 /*! @brief Identifiers for various output messages.
63  */
64 typedef enum MessageType {
66 } MessageType;
67 
68 
69 // **************************************************************
70 /*! @brief System structure for various global variables.
71  */
72 typedef struct SystemInfo {
73  int verbosity; // system printf verbosity
74  int pause_after_run; // to keep terminal open on Windows
75  string log_fname; // name of the master log file
76  FILE *log; // log file handle
77 
78  int n_proc; // number of processors
79  int my_proc; // self processor number
80 
82 
83 } SystemInfo;
84 
85 extern SystemInfo sys_info;
86 
87 /*
88 char * get_log_fname( void );
89 char * get_log_file( void );
90 void resume_log_file( void );
91 */
92 
93 #define xprintf(...) _xprintf(__FILE__, __func__, __LINE__, __VA_ARGS__)
94 
95 int _xprintf(const char * const xprintf_file, const char * const xprintf_func, const int xprintf_line, MessageType type, const char * const fmt, ... );
96 //void * xmalloc(size_t size);
97 //void * xrealloc( void * ptr, size_t size );
98 
99 // TODO: implement as a templated function
100 /*
101 #ifndef xfree
102  #define xfree(p) \
103  do { if (p) { free(p); (p)=NULL; } \
104  } while (0) /// test & free memory
105 #endif
106 */
107 /**
108  * @brief Replacement of new/delete operator in the spirit of xmalloc.
109  *
110  * Up to my knowledge overloading of original new/delete is the only clean.
111  * Possibly disadvantage is that all 'new' calls in system and other templates
112  * become also overloaded.
113  *
114  */
115 // @{
116 /*
117 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
118 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
119 void operator delete( void *p) throw();
120 void operator delete[]( void *p) throw();
121 */
122 // @}
123 
124 //int xsystem(const char*);
125 
126 //! @brief Operations on files and directories
127 /// @{
128 /*
129 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
130 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
131 int xremove( const char *s ); ///< Remove file or directory (function)
132 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
133 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
134 */
135 //! @}
136 
137 // string operations
138 /*
139 char * xstrcpy(const char*);
140 char * xstrtok(char *s, int position = -1);
141 char * xstrtok(char*,const char* delim, int position = -1);
142 int xchomp( char * s );
143 */
144 
145 /**
146  * Wrapper to check return codes of C functions. In particular PETSC calls.
147  */
148 inline void chkerr(unsigned int ierr) {
149  do {
150  if (ierr != 0) THROW( ExcChkErr() << EI_ErrCode(ierr));
151  } while (0);
152 }
153 
154 /**
155  * Wrapper to check return codes of C functions. In particular PETSC calls.
156  * This version do the check just as an debugging assert. So the code is empty
157  * in release version.
158  */
159 inline void chkerr_assert(unsigned int ierr) {
160 #ifdef FLOW123D_DEBUG_ASSERTS
161  chkerr(ierr);
162 #else
163  (void)ierr; // not used
164 #endif
165 }
166 
167 #endif
168 //-----------------------------------------------------------------------------
169 // vim: set cindent:
int my_proc
Definition: system.hh:79
Definition: system.hh:65
struct SystemInfo SystemInfo
System structure for various global variables.
int MPI_Comm
Definition: mpi.h:141
FILE * log
Definition: system.hh:76
int verbosity
Definition: system.hh:73
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:148
Definition: system.hh:65
System structure for various global variables.
Definition: system.hh:72
string log_fname
Definition: system.hh:75
Global macros to enhance readability and debugging, general constants.
Definition: system.hh:65
void chkerr_assert(unsigned int ierr)
Definition: system.hh:159
Definition: system.hh:65
SystemInfo sys_info
Definition: system.cc:41
Definition: system.hh:65
MessageType
Identifiers for various output messages.
Definition: system.hh:64
Definition: system.hh:65
int _xprintf(const char *const xprintf_file, const char *const xprintf_func, const int xprintf_line, MessageType type, const char *const fmt,...)
Multi-purpose printing routine: messages, warnings, errors.
Definition: system.cc:85
int n_proc
Definition: system.hh:78
Definition: format.cc:82
Definition: system.hh:65
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
MPI_Comm comm
Definition: system.hh:81
int pause_after_run
Definition: system.hh:74