From dde75377f2cef774a3fb7d5ec0001ecd04744ab2 Mon Sep 17 00:00:00 2001 From: Dengda98 Date: Thu, 4 Sep 2025 23:24:55 +0800 Subject: [PATCH] REFAC: use macro `GRTRaiseError` --- .../include/grt/common/checkerror.h | 7 +++- pygrt/C_extension/src/common/model.c | 37 +++++++------------ pygrt/C_extension/src/common/sacio2.c | 9 ++--- pygrt/C_extension/src/common/util.c | 8 ++-- pygrt/C_extension/src/dynamic/grt_greenfn.c | 3 +- pygrt/C_extension/src/dynamic/grt_rotation.c | 2 +- pygrt/C_extension/src/dynamic/grt_strain.c | 2 +- pygrt/C_extension/src/dynamic/grt_stress.c | 4 +- pygrt/C_extension/src/dynamic/layer.c | 8 ++-- pygrt/C_extension/src/dynamic/signals.c | 37 +++++++------------ 10 files changed, 49 insertions(+), 68 deletions(-) diff --git a/pygrt/C_extension/include/grt/common/checkerror.h b/pygrt/C_extension/include/grt/common/checkerror.h index b22c5f87..1c3d76fc 100644 --- a/pygrt/C_extension/include/grt/common/checkerror.h +++ b/pygrt/C_extension/include/grt/common/checkerror.h @@ -18,10 +18,15 @@ // GRT自定义报错信息 #define GRTRaiseError(ErrorMessage, ...) ({\ - fprintf(stderr, BOLD_RED ErrorMessage DEFAULT_RESTORE, ##__VA_ARGS__);\ + fprintf(stderr, BOLD_RED ErrorMessage "\n" DEFAULT_RESTORE, ##__VA_ARGS__);\ exit(EXIT_FAILURE);\ }) +// GRT自定义警告信息,不结束程序 +#define GRTRaiseWarning(WarnMessage, ...) ({\ + fprintf(stderr, BOLD_YELLOW WarnMessage "\n" DEFAULT_RESTORE, ##__VA_ARGS__);\ +}) + // GRT报错:选项设置不符要求 #define GRTBadOptionError(name, X, MoreErrorMessage, ...) ({\ GRTRaiseError("[%s] Error in \"-"#X"\". "MoreErrorMessage" Use \"-h\" for help.\n", name, ##__VA_ARGS__);\ diff --git a/pygrt/C_extension/src/common/model.c b/pygrt/C_extension/src/common/model.c index e36a0567..430c9599 100755 --- a/pygrt/C_extension/src/common/model.c +++ b/pygrt/C_extension/src/common/model.c @@ -15,7 +15,6 @@ #include "grt/common/model.h" #include "grt/common/prtdbg.h" #include "grt/common/attenuation.h" -#include "grt/common/colorstr.h" #include "grt/common/util.h" #include "grt/common/checkerror.h" @@ -230,8 +229,7 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat h = va = vb = rho = qa = qb = 0.0; MYINT nscan = sscanf(line, "%lf %lf %lf %lf %lf %lf\n", &h, &va, &vb, &rho, &qa, &qb); if(ncols != nscan && ncols_noQ != nscan){ - fprintf(stderr, "[%s] " BOLD_RED "Model file read error in line %d.\n" DEFAULT_RESTORE, command, iline); - return NULL; + GRTRaiseError("[%s] Model file read error in line %d.\n", command, iline); }; // 读取首行,如果首行首列为 0 ,则首列指示每层顶界面深度而非厚度 @@ -240,18 +238,15 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat } if(va <= 0.0 || rho <= 0.0 || (ncols == nscan && (qa <= 0.0 || qb <= 0.0))){ - fprintf(stderr, "[%s] " BOLD_RED "In model file, line %d, nonpositive value is not supported.\n" DEFAULT_RESTORE, command, iline); - return NULL; + GRTRaiseError("[%s] In model file, line %d, nonpositive value is not supported.\n", command, iline); } if(vb < 0.0){ - fprintf(stderr, "[%s] " BOLD_RED "In model file, line %d, negative Vs is not supported.\n" DEFAULT_RESTORE, command, iline); - return NULL; + GRTRaiseError("[%s] In model file, line %d, negative Vs is not supported.\n", command, iline); } if(!allowLiquid && vb == 0.0){ - fprintf(stderr, "[%s] " BOLD_RED "In model file, line %d, Vs==0.0 is not supported.\n" DEFAULT_RESTORE, command, iline); - return NULL; + GRTRaiseError("[%s] In model file, line %d, Vs==0.0 is not supported.\n", command, iline); } modarr = (double(*)[ncols])realloc(modarr, sizeof(double)*ncols*(nlay+1)); @@ -267,8 +262,7 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat } if(iline==0 || modarr==NULL){ - fprintf(stderr, "[%s] " BOLD_RED "Model file read error.\n" DEFAULT_RESTORE, command); - return NULL; + GRTRaiseError("[%s] Model file %s read error.\n", command, modelpath); } // 如果读取了深度,转为厚度 @@ -293,9 +287,8 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat // 允许最后一层厚度为任意值 if(h <= 0.0 && i < nlay0-1 ) { - fprintf(stderr, "[%s] " BOLD_RED "In line %d, nonpositive thickness (except last layer)" - " is not supported.\n" DEFAULT_RESTORE, command, i+1); - return NULL; + GRTRaiseError("[%s] In line %d, nonpositive thickness (except last layer)" + " is not supported.\n", command, i+1); } // 划分震源层和接收层 @@ -345,24 +338,20 @@ GRT_MODEL1D * grt_read_mod1d_from_file(const char *command, const char *modelpat // 检查,接收点不能位于液-液、固-液界面 if(ircv < nlay-1 && mod1d->Thk[ircv] == 0.0 && mod1d->Vb[ircv]*mod1d->Vb[ircv+1] == 0.0){ - fprintf(stderr, - "[%s] " BOLD_RED "The receiver is located on the interface where there is liquid on one side. " + GRTRaiseError( + "[%s] The receiver is located on the interface where there is liquid on one side. " "Due to the discontinuity of the tangential displacement on this interface, " "to reduce ambiguity, you should add a small offset to the receiver depth, " - "thereby explicitly placing it within a specific layer. \n" - DEFAULT_RESTORE, command); - return NULL; + "thereby explicitly placing it within a specific layer. \n", command); } // 检查 --> 源点不能位于液-液、固-液界面 if(isrc < nlay-1 && mod1d->Thk[isrc] == 0.0 && mod1d->Vb[isrc]*mod1d->Vb[isrc+1] == 0.0){ - fprintf(stderr, - "[%s] " BOLD_RED "The source is located on the interface where there is liquid on one side. " + GRTRaiseError( + "[%s] The source is located on the interface where there is liquid on one side. " "Due to the discontinuity of the tangential displacement on this interface, " "to reduce ambiguity, you should add a small offset to the source depth, " - "thereby explicitly placing it within a specific layer. \n" - DEFAULT_RESTORE, command); - return NULL; + "thereby explicitly placing it within a specific layer. \n", command); } // 将每层顶界面深度写入数组 diff --git a/pygrt/C_extension/src/common/sacio2.c b/pygrt/C_extension/src/common/sacio2.c index d371aed3..314f93a6 100644 --- a/pygrt/C_extension/src/common/sacio2.c +++ b/pygrt/C_extension/src/common/sacio2.c @@ -12,14 +12,14 @@ #include "grt/common/sacio2.h" #include "grt/common/sacio.h" -#include "grt/common/colorstr.h" + +#include "grt/common/checkerror.h" void grt_read_SAC_HEAD(const char *command, const char *name, SACHEAD *hd){ int lswap = read_sac_head(name, hd); if(lswap == -1){ - fprintf(stderr, "[%s] " BOLD_RED "read %s head failed.\n" DEFAULT_RESTORE, command, name); - exit(EXIT_FAILURE); + GRTRaiseError("[%s] read %s head failed.\n", command, name); } } @@ -27,8 +27,7 @@ void grt_read_SAC_HEAD(const char *command, const char *name, SACHEAD *hd){ float * grt_read_SAC(const char *command, const char *name, SACHEAD *hd, float *arrout){ float *arrin=NULL; if((arrin = read_sac(name, hd)) == NULL){ - fprintf(stderr, "[%s] " BOLD_RED "read %s failed.\n" DEFAULT_RESTORE, command, name); - exit(EXIT_FAILURE); + GRTRaiseError("[%s] read %s failed.\n", command, name); } if(arrout!=NULL){ diff --git a/pygrt/C_extension/src/common/util.c b/pygrt/C_extension/src/common/util.c index fadaa9e3..6dcebf77 100644 --- a/pygrt/C_extension/src/common/util.c +++ b/pygrt/C_extension/src/common/util.c @@ -19,6 +19,8 @@ #include "grt/common/myfftw.h" #include "grt/travt/travt.h" +#include "grt/common/checkerror.h" + char ** grt_string_split(const char *string, const char *delim, int *size) { char *str_copy = strdup(string); // 创建字符串副本,以免修改原始字符串 @@ -388,10 +390,10 @@ void grt_GF_freq2time_write_to_file( // 输出警告:当震源位于液体层中时,仅允许计算爆炸源对应的格林函数 if(mod1d->Vb[mod1d->isrc]==0.0){ - fprintf(stderr, "[%s] " BOLD_YELLOW - "The source is located in the liquid layer, " + GRTRaiseWarning( + "[%s] The source is located in the liquid layer, " "therefore only the Green's Funtions for the Explosion source will be computed.\n" - DEFAULT_RESTORE, command); + , command); } } \ No newline at end of file diff --git a/pygrt/C_extension/src/dynamic/grt_greenfn.c b/pygrt/C_extension/src/dynamic/grt_greenfn.c index 03a7890b..02ee92cc 100644 --- a/pygrt/C_extension/src/dynamic/grt_greenfn.c +++ b/pygrt/C_extension/src/dynamic/grt_greenfn.c @@ -631,8 +631,7 @@ static void getopt_from_command(GRT_MODULE_CTRL *Ctrl, int argc, char **argv){ int i1, i2, i3, i4; i1 = i2 = i3 = i4 = 0; if(0 == sscanf(optarg, "%d/%d/%d/%d", &i1, &i2, &i3, &i4)){ - fprintf(stderr, "[%s] " BOLD_RED "Error in -G.\n" DEFAULT_RESTORE, command); - exit(EXIT_FAILURE); + GRTBadOptionError(command, G, ""); }; Ctrl->G.doEX = (i1!=0); Ctrl->G.doVF = (i2!=0); diff --git a/pygrt/C_extension/src/dynamic/grt_rotation.c b/pygrt/C_extension/src/dynamic/grt_rotation.c index c7268678..75452840 100644 --- a/pygrt/C_extension/src/dynamic/grt_rotation.c +++ b/pygrt/C_extension/src/dynamic/grt_rotation.c @@ -78,7 +78,7 @@ int rotation_main(int argc, char **argv){ // 保存文件前缀 Ctrl->s_prefix = (char*)malloc(sizeof(char)*(strlen(Ctrl->s_dirpath)+1)); if(2 != sscanf(Ctrl->s_dirpath, "%[^/]/%s", Ctrl->s_synpath, Ctrl->s_prefix)){ - GRTRaiseError("[%s] " BOLD_RED "Error format in \"%s\".\n" DEFAULT_RESTORE, command, Ctrl->s_dirpath); + GRTRaiseError("[%s] Error format in \"%s\".\n", command, Ctrl->s_dirpath); } // 检查是否存在该目录 diff --git a/pygrt/C_extension/src/dynamic/grt_strain.c b/pygrt/C_extension/src/dynamic/grt_strain.c index a3e8cfd0..59bff4c3 100644 --- a/pygrt/C_extension/src/dynamic/grt_strain.c +++ b/pygrt/C_extension/src/dynamic/grt_strain.c @@ -77,7 +77,7 @@ int strain_main(int argc, char **argv){ // 保存文件前缀 Ctrl->s_prefix = (char*)malloc(sizeof(char)*(strlen(Ctrl->s_dirpath)+1)); if(2 != sscanf(Ctrl->s_dirpath, "%[^/]/%s", Ctrl->s_synpath, Ctrl->s_prefix)){ - GRTRaiseError("[%s] " BOLD_RED "Error format in \"%s\".\n" DEFAULT_RESTORE, command, Ctrl->s_dirpath); + GRTRaiseError("[%s] Error format in \"%s\".\n", command, Ctrl->s_dirpath); } // 检查是否存在该目录 diff --git a/pygrt/C_extension/src/dynamic/grt_stress.c b/pygrt/C_extension/src/dynamic/grt_stress.c index 52591e72..28925a33 100644 --- a/pygrt/C_extension/src/dynamic/grt_stress.c +++ b/pygrt/C_extension/src/dynamic/grt_stress.c @@ -79,7 +79,7 @@ int stress_main(int argc, char **argv){ // 保存文件前缀 Ctrl->s_prefix = (char*)malloc(sizeof(char)*(strlen(Ctrl->s_dirpath)+1)); if(2 != sscanf(Ctrl->s_dirpath, "%[^/]/%s", Ctrl->s_synpath, Ctrl->s_prefix)){ - GRTRaiseError("[%s] " BOLD_RED "Error format in \"%s\".\n" DEFAULT_RESTORE, command, Ctrl->s_dirpath); + GRTRaiseError("[%s] Error format in \"%s\".\n", command, Ctrl->s_dirpath); } // 检查是否存在该目录 @@ -119,7 +119,7 @@ int stress_main(int argc, char **argv){ float Qainv=hd.user4; float Qbinv=hd.user5; if(va <= 0.0 || vb < 0.0 || rho <= 0.0){ - fprintf(stderr, "[%s] " BOLD_RED "Error! Bad rcv_va, rcv_vb or rcv_rho in \"%s\" header.\n" DEFAULT_RESTORE, command, s_filepath); + fprintf(stderr, "[%s] Error! Bad rcv_va, rcv_vb or rcv_rho in \"%s\" header.\n", command, s_filepath); exit(EXIT_FAILURE); } // 申请内存 diff --git a/pygrt/C_extension/src/dynamic/layer.c b/pygrt/C_extension/src/dynamic/layer.c index 00081d90..c89cd38e 100644 --- a/pygrt/C_extension/src/dynamic/layer.c +++ b/pygrt/C_extension/src/dynamic/layer.c @@ -20,8 +20,8 @@ #include "grt/common/model.h" #include "grt/common/prtdbg.h" #include "grt/common/matrix.h" -#include "grt/common/colorstr.h" +#include "grt/common/checkerror.h" void grt_calc_R_tilt_PSV(MYCOMPLEX xa0, MYCOMPLEX xb0, MYCOMPLEX kbkb0, MYREAL k, MYCOMPLEX R_tilt[2][2], MYINT *stats) { @@ -214,8 +214,7 @@ void grt_calc_RT_ls_PSV( bool isfluidUp = (mu1 == 0.0); // 上层是否为液体 MYINT sgn = 1; if(isfluidUp && mu2 == 0.0){ - fprintf(stderr, BOLD_RED "Error: fluid-fluid interface is not allowed in calc_RT_ls_2x2\n" DEFAULT_RESTORE); - exit(EXIT_FAILURE); + GRTRaiseError("Error: fluid-fluid interface is not allowed in function %s\n", __func__); } // 使用指针 @@ -299,8 +298,7 @@ void grt_calc_RT_ls_SH( // 讨论液-固 or 固-液 bool isfluidUp = (mu1 == 0.0); // 上层是否为液体 if(isfluidUp && mu2 == 0.0){ - fprintf(stderr, BOLD_RED "Error: fluid-fluid interface is not allowed in calc_RT_ls_2x2\n" DEFAULT_RESTORE); - exit(EXIT_FAILURE); + GRTRaiseError("Error: fluid-fluid interface is not allowed in function %s\n", __func__); } // 使用指针 diff --git a/pygrt/C_extension/src/dynamic/signals.c b/pygrt/C_extension/src/dynamic/signals.c index b481d21f..f78afe63 100644 --- a/pygrt/C_extension/src/dynamic/signals.c +++ b/pygrt/C_extension/src/dynamic/signals.c @@ -21,9 +21,10 @@ #include "grt/dynamic/signals.h" #include "grt/common/const.h" -#include "grt/common/colorstr.h" #include "grt/common/util.h" +#include "grt/common/checkerror.h" + bool grt_check_tftype_tfparams(const char tftype, const char *tfparams){ @@ -32,8 +33,7 @@ bool grt_check_tftype_tfparams(const char tftype, const char *tfparams){ float t0=0.0; if(1 != sscanf(tfparams, "%f", &t0)) return false; if(t0 <= 0){ - fprintf(stderr, BOLD_RED "Error! t0(%s) should be larger than 0.\n" DEFAULT_RESTORE, tfparams); - return false; + GRTRaiseError("Error! t0(%s) should be larger than 0.\n", tfparams); } } // 梯形波 @@ -41,12 +41,10 @@ bool grt_check_tftype_tfparams(const char tftype, const char *tfparams){ float t1=0.0, t2=0.0, t3=0.0; if(3 != sscanf(tfparams, "%f/%f/%f", &t1, &t2, &t3)) return false; if(t1 < 0.0 || t2 < 0.0 || t3 <= 0.0){ - fprintf(stderr, BOLD_RED "Error! It should be t1>=0.0, t2>=0.0 and t3>0.0 (%s).\n" DEFAULT_RESTORE, tfparams); - return false; + GRTRaiseError("Error! It should be t1>=0.0, t2>=0.0 and t3>0.0 (%s).\n", tfparams); } if(! (t1 <= t2 && t2 < t3)){ - fprintf(stderr, BOLD_RED "Error! It should be t1<=t2