Changeset 495 for cpp


Ignore:
Timestamp:
04/11/16 02:07:03 (8 years ago)
Author:
Maciej Komosinski
Message:

Unified and better formatted error and warning messages

Location:
cpp/frams/model
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/model/model.cpp

    r458 r495  
    326326                }
    327327        mh.reset();
    328         if (singleStepBuild(line,autobuildmaps ? (&frommap) : 0)==-1)
     328        if (singleStepBuild(line,lnum,autobuildmaps ? (&frommap) : 0) == -1)
    329329                {
    330330                buildstatus=invalid;
    331                 logPrintf("Model","build",LOG_ERROR,
    332                          geno.getName().len()?"illegal f0 code at line %d (%s)":"illegal f0 code at line %d",
    333                          lnum,geno.getName().c_str());
    334331                f0errorposition=lastpos;
    335332                if (convmap) delete convmap;
     
    407404modelparam.select(this);
    408405modelparam.save2(mod_props,handle_defaults ? &defaultmodel : NULL,true,!handle_defaults);
    409 if (mod_props.len()>0) //are there any non-default values?
     406if (mod_props.len()>1) //are there any non-default values? ("\n" is empty)
    410407        {
    411408        gen+="m:";
     
    513510{
    514511if (buildstatus!=building)
    515         logPrintf("Model","close",LOG_WARN,"unexpected close() - no open()");
     512        logPrintf("Model","close",LOG_WARN,"Unexpected close() - no open()");
    516513if (internalcheck(1)>0)
    517514        {
     
    557554}
    558555
    559 int Model::singleStepBuild(const SString &line,const MultiRange* srcrange)
    560 {
     556int Model::singleStepBuild(const SString &singleline,const MultiRange* srcrange)
     557{
     558return singleStepBuild(singleline,0,srcrange);
     559}
     560
     561int Model::singleStepBuild(const SString &singleline,int line_num,const MultiRange* srcrange)
     562{
     563SString error_message;
     564int result=singleStepBuildNoLog(singleline,error_message,srcrange);
     565if (result<0)
     566        {
     567        if (error_message.len()==0) // generic error when no detailed message is available
     568                error_message="Invalid f0 code";
     569        if (line_num>0)
     570                error_message+=SString::sprintf(", line #%d",line_num);
     571        error_message+=nameForErrors();
     572        logPrintf("Model","build",LOG_ERROR,"%s",error_message.c_str());
     573        }
     574return result;
     575}
     576
     577int Model::singleStepBuildNoLog(const SString &line,SString& error_message,const MultiRange* srcrange)
     578{
     579error_message=SString::empty();
    561580int pos=0; const char*t=line.c_str();
    562581for (;*t;t++,pos++)
     
    570589        partparam.select(p);
    571590        pos+=2;
    572         if (partparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) {delete p; return -1;}
     591        if (partparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) {delete p; error_message="Invalid 'p:'"; return -1;}
    573592        p->o.rotate(p->rot);
    574593        parts+=p;
     
    582601        modelparam.select(this);
    583602        pos+=2;
    584         if (modelparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) return -1;
     603        if (modelparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) { error_message="Invalid 'm:'"; return -1;}
    585604        return 0;
    586605        }
     
    592611        pos+=2;
    593612        j->owner=this;
    594         if (jointparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) {delete j; return -1;}
    595         if ((j->p1_refno>=0)&&(j->p1_refno<getPartCount())&&
    596            (j->p2_refno>=0)&&(j->p2_refno<getPartCount()))
     613        if (jointparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) {delete j; error_message="Invalid 'j:'"; return -1;}
     614        bool p1_ok=false, p2_ok=false;
     615        if ((p1_ok=((j->p1_refno>=0)&&(j->p1_refno<getPartCount())))&&
     616            (p2_ok=((j->p2_refno>=0)&&(j->p2_refno<getPartCount()))))
    597617                {
    598618                addJoint(j);
     
    609629                {
    610630                delete j;
    611                 logPrintf("Model","build",LOG_ERROR,
    612                          "invalid part reference for joint #%d",getJointCount()-1);
     631                error_message=SString::sprintf("Invalid reference to Part #%d",p1_ok?j->p1_refno:j->p2_refno);
    613632                return -1;
    614633                }
     
    620639        neuroparam.select(nu);
    621640        pos+=2;
    622         if (neuroparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) {delete nu; return -1;}
     641        if (neuroparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) {delete nu; error_message="Invalid 'n:'"; return -1;}
    623642#ifdef MODEL_V1_COMPATIBLE
    624643        if (nu->neuro_refno>=0) // parent specified...
     
    684703        ncparam.select(&c);
    685704        pos+=2;
    686         if (ncparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) return -1;
    687         if ((c.n1_refno>=0)&&(c.n1_refno<getNeuroCount())&&(c.n2_refno>=0)&&(c.n2_refno<getNeuroCount()))
     705        if (ncparam.load2(line,pos) & ParamInterface::LOAD2_PARSE_FAILED) { error_message="Invalid 'c:'"; return -1; }
     706        bool n1_ok=false,n2_ok=false;
     707        if ((n1_ok=((c.n1_refno>=0)&&(c.n1_refno<getNeuroCount())))
     708            && (n2_ok=((c.n2_refno>=0)&&(c.n2_refno<getNeuroCount()))))
    688709                {
    689710                Neuro *na=getNeuro(c.n1_refno);
     
    694715                return 0;
    695716                }
    696         logPrintf("Model","build",LOG_ERROR,
    697                  "invalid neuron connection #%d <- #%d",c.n1_refno,c.n2_refno);
     717        error_message=SString::sprintf("Invalid reference to Neuro #%d",n1_ok ? c.n2_refno : c.n1_refno);
    698718        return -1;
    699719        }
     
    783803                                {
    784804                                logPrintf("Model","internalCheck",LOG_ERROR,
    785                                          "illegal N-N connection #%d (reference to #%d) (%s)",
    786                                          i,n->conn_refno,(const char*)geno.getName());
     805                                         "illegal N-N connection #%d (reference to #%d)%s",
     806                                          i,n->conn_refno,nameForErrors().c_str());
    787807                                return 0;
    788808                                }
     
    853873if (var -> field < getMin ## template () . field) \
    854874        { var->field= getMin ## template () . field; \
    855         logPrintf("Model","internalCheck",LOG_WARN,# field " too small in " # template "#%d (adjusted)",i);} \
     875        logPrintf("Model","internalCheck",LOG_WARN,# field " too small in " # template " #%d (adjusted)",i);} \
    856876else if (var -> field > getMax ## template () . field) \
    857877        { var->field= getMax ## template ()  . field; \
    858         logPrintf("Model","internalCheck",LOG_WARN,# field " too big in " # template "#%d (adjusted)",i);}
     878        logPrintf("Model","internalCheck",LOG_WARN,# field " too big in " # template " #%d (adjusted)",i);}
    859879
    860880#define LINKFLAG 0x8000000
     
    866886for (i=0;i<getJointCount();i++) getJoint(i)->refno=i;
    867887for (i=0;i<getNeuroCount();i++) getNeuro(i)->refno=i;
     888}
     889
     890SString Model::nameForErrors() const
     891{
     892if (geno.getName().len()>0)
     893        return SString::sprintf(" in '%s'",geno.getName().c_str());
     894return SString::empty();
    868895}
    869896
     
    912939                        {
    913940                        shape=SHAPE_ILLEGAL;
    914                         logPrintf("Model","internalCheck",LOG_WARN,"Inconsistent part shapes (mixed old and new shapes)");
     941                        logPrintf("Model","internalCheck",LOG_WARN,"Inconsistent part shapes (mixed old and new shapes)%s",nameForErrors().c_str());
    915942                        }
    916943                }
     
    942969                                ret=0;
    943970                                logPrintf("Model","internalCheck",LOG_ERROR,
    944                                          "delta joint cycle detected at joint#%d (%s)",
    945                                          i,geno.getName().c_str());
     971                                         "Delta joint cycle detected at Joint #%d%s",
     972                                         i,nameForErrors().c_str());
    946973                                }
    947974                        j->resetDeltaMarkers();
     
    9781005                        {
    9791006                        ret=0;
    980                         logPrintf("Model","internalCheck",LOG_ERROR,"delta too big in joint #%d (%s)",
    981                                  i,geno.getName().c_str());
     1007                        logPrintf("Model","internalCheck",LOG_ERROR,"Delta too big in Joint #%d%s",i,nameForErrors().c_str());
    9821008                        }
    9831009                        else if (j->d()<getMinJoint().d.x)
    9841010                        {
    9851011                        ret=0;
    986                         logPrintf("Model","internalCheck",LOG_ERROR,"delta too small in joint #%d (%s)",
    987                                  i,geno.getName().c_str());
     1012                        logPrintf("Model","internalCheck",LOG_ERROR,"delta too small in Joint #%d%s",i,nameForErrors().c_str());
    9881013                        }
    9891014                                }
     
    9921017        else
    9931018                {
    994                 logPrintf("Model","internalCheck",LOG_ERROR,"illegal part references in joint #%d (%s)",
    995                          i,geno.getName().c_str());
     1019                logPrintf("Model","internalCheck",LOG_ERROR,"Illegal part references in Joint #%d%s",i,nameForErrors().c_str());
    9961020                ret=0;
    9971021                }
     
    10011025                        {
    10021026                        shape=SHAPE_ILLEGAL;
    1003                         logPrintf("Model","internalCheck",LOG_WARN,"Inconsistent joint shapes (mixed old and new shapes)");
     1027                        logPrintf("Model","internalCheck",LOG_WARN,"Inconsistent joint shapes (mixed old and new shapes)%s",nameForErrors().c_str());
    10041028                        }
    10051029                }
     
    10711095                if (!(p->flags&LINKFLAG))
    10721096                        {
    1073                         logPrintf("Model","internalCheck",LOG_ERROR,"not all parts connected (eg.#0-#%d) (%s)",
    1074                                  i,geno.getName().c_str());
     1097                        logPrintf("Model","internalCheck",LOG_ERROR,"Not all parts connected (eg. Part #0 and Part #%d)%s",i,nameForErrors().c_str());
    10751098                        ret=0;
    10761099                        break;
     
    10841107        if (j->p1_refno==j->p2_refno)
    10851108                {
    1086                 logPrintf("Model","internalCheck",LOG_ERROR,"illegal self connection, joint #%d (%s)",
    1087                          i,geno.getName().c_str());
     1109                logPrintf("Model","internalCheck",LOG_ERROR,"Illegal self connection, Joint #%d%s",i,nameForErrors().c_str());
    10881110                ret=0;
    10891111                break;
     
    10951117                    || ((j->p1_refno==j2->p2_refno)&&(j->p2_refno==j2->p1_refno)))
    10961118                        {
    1097                         logPrintf("Model","internalCheck",LOG_ERROR,"illegal duplicate joints #%d and #%d (%s)",
    1098                                  i,k,geno.getName().c_str());
     1119                        logPrintf("Model","internalCheck",LOG_ERROR,"Illegal duplicate Joint #%d and Joint #%d%s",i,k,nameForErrors().c_str());
    10991120                        ret=0;
    11001121                        break;
     
    11251146{
    11261147if (buildstatus==building)
    1127         logPrintf("Model","getGeno",LOG_WARN,"model was not completed - missing close()");
     1148        logPrintf("Model","getGeno",LOG_WARN,"Model was not completed - missing close()");
    11281149if (buildstatus!=valid)
    11291150        return Geno("",'0',"","invalid");
  • cpp/frams/model/model.h

    r408 r495  
    124124
    125125void updateNeuroRefno(); // set Neuro::refno for all neurons
     126SString nameForErrors() const;
    126127int internalcheck(int final);
    127128
     
    275276    or negative value. reference number can be used to access
    276277    the item using getPart(int), getJoint(int) and getNeuroItem(int) methods.
     278    @param line_num optional line number used in error messages
    277279    @param srcrange source genotype range which will be mapped to this element
    278280*/
    279 int singleStepBuild(const SString &singleline,const MultiRange* srcrange=0);
     281int singleStepBuild(const SString &singleline,int line_num,const MultiRange* srcrange=NULL);
     282/** Execute single line of <B>f0</B> genotype - compatiblity variant */
     283int singleStepBuild(const SString &singleline,const MultiRange* srcrange=NULL);
     284/** Execute single line of <B>f0</B> genotype - low level variant, used by Model::build(), error messages returned as string instead of calling logger */
     285int singleStepBuildNoLog(const SString &singleline,SString& error_message,const MultiRange* srcrange=0);
    280286
    281287/// separate build stages (for future use)
Note: See TracChangeset for help on using the changeset viewer.