Flow123d  release_2.2.0-914-gf1a3a4f
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 
33 
34 #ifndef _BSD_SOURCE
35  #define _BSD_SOURCE
36 #endif
37 
38 #ifdef WINDOWS
39  #include <direct.h>
40  #define GetCurrentDir _getcwd
41  #define PATH_MAX MAX_PATH
42 #else
43  //#include <unistd.h>
44  //#define GetCurrentDir getcwd
45  //#include <limits.h> //PATH_MAX
46 #endif
47 
48 #define strcmpi strcasecmp
49 #define DIR_DELIMITER '/'
50 
51 
52 // Assuming all compilers supports CXX11 features
53 #define OPERATOR_NEW_THROW_EXCEPTION
54 
55 
56 
57 using namespace std;
58 
59 
60 // **************************************************************
61 /*! @brief Identifiers for various output messages.
62  */
63 typedef enum MessageType {
65 } MessageType;
66 
67 
68 // **************************************************************
69 /*! @brief System structure for various global variables.
70  */
71 typedef struct SystemInfo {
72  int verbosity; // system printf verbosity
73  int pause_after_run; // to keep terminal open on Windows
74  string log_fname; // name of the master log file
75  FILE *log; // log file handle
76 
77  int n_proc; // number of processors
78  int my_proc; // self processor number
79 
81 
82 } SystemInfo;
83 
84 extern SystemInfo sys_info;
85 
86 /*
87 char * get_log_fname( void );
88 char * get_log_file( void );
89 void resume_log_file( void );
90 */
91 
92 #define xprintf(...) _xprintf(__FILE__, __func__, __LINE__, __VA_ARGS__)
93 
94 int _xprintf(const char * const xprintf_file, const char * const xprintf_func, const int xprintf_line, MessageType type, const char * const fmt, ... );
95 //void * xmalloc(size_t size);
96 //void * xrealloc( void * ptr, size_t size );
97 
98 // TODO: implement as a templated function
99 /*
100 #ifndef xfree
101  #define xfree(p) \
102  do { if (p) { free(p); (p)=NULL; } \
103  } while (0) /// test & free memory
104 #endif
105 */
106 /**
107  * @brief Replacement of new/delete operator in the spirit of xmalloc.
108  *
109  * Up to my knowledge overloading of original new/delete is the only clean.
110  * Possibly disadvantage is that all 'new' calls in system and other templates
111  * become also overloaded.
112  *
113  */
114 // @{
115 /*
116 void *operator new (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
117 void *operator new[] (std::size_t size) OPERATOR_NEW_THROW_EXCEPTION;
118 void operator delete( void *p) throw();
119 void operator delete[]( void *p) throw();
120 */
121 // @}
122 
123 //int xsystem(const char*);
124 
125 //! @brief Operations on files and directories
126 /// @{
127 /*
128 int xmkdir( const char *s ); ///< Create directory (GLIBC function, original in <sys/stat.h>)
129 int xchdir( const char *s ); ///< Change directory (GLIBC function, original in <unistd.h>)
130 int xremove( const char *s ); ///< Remove file or directory (function)
131 char * xgetcwd( void ); ///< Get current working directory (GLIBC function, original in <unistd.h>)
132 int xrename( const char * oldname, const char * newname ); ///< Rename file (function)
133 */
134 //! @}
135 
136 // string operations
137 /*
138 char * xstrcpy(const char*);
139 char * xstrtok(char *s, int position = -1);
140 char * xstrtok(char*,const char* delim, int position = -1);
141 int xchomp( char * s );
142 */
143 
144 /**
145  * Wrapper to check return codes of C functions. In particular PETSC calls.
146  */
147 inline void chkerr(unsigned int ierr) {
148  do {
149  if (ierr != 0) THROW( ExcChkErr() << EI_ErrCode(ierr));
150  } while (0);
151 }
152 
153 /**
154  * Wrapper to check return codes of C functions. In particular PETSC calls.
155  * This version do the check just as an debugging assert. So the code is empty
156  * in release version.
157  */
158 inline void chkerr_assert(unsigned int ierr) {
159 #ifdef FLOW123D_DEBUG_ASSERTS
160  chkerr(ierr);
161 #endif
162 }
163 
164 #endif
165 //-----------------------------------------------------------------------------
166 // vim: set cindent:
int my_proc
Definition: system.hh:78
Definition: system.hh:64
struct SystemInfo SystemInfo
System structure for various global variables.
int MPI_Comm
Definition: mpi.h:141
FILE * log
Definition: system.hh:75
int verbosity
Definition: system.hh:72
void chkerr(unsigned int ierr)
Replacement of new/delete operator in the spirit of xmalloc.
Definition: system.hh:147
Definition: system.hh:64
System structure for various global variables.
Definition: system.hh:71
string log_fname
Definition: system.hh:74
Global macros to enhance readability and debugging, general constants.
Definition: system.hh:64
void chkerr_assert(unsigned int ierr)
Definition: system.hh:158
Definition: system.hh:64
SystemInfo sys_info
Definition: system.cc:41
Definition: system.hh:64
MessageType
Identifiers for various output messages.
Definition: system.hh:63
Definition: system.hh:64
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:77
Definition: format.cc:82
Definition: system.hh:64
#define THROW(whole_exception_expr)
Wrapper for throw. Saves the throwing point.
Definition: exceptions.hh:53
MPI_Comm comm
Definition: system.hh:80
int pause_after_run
Definition: system.hh:73