Author Topic: Macros for error messages  (Read 4219 times)

0 Members and 1 Guest are viewing this topic.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Macros for error messages
« on: April 03, 2009, 12:31:08 PM »
Does anybody use macros to make things a little easier checking the status of functions?
If so do you have a cool one to share?

I.e.

Code: [Select]
#define MSGNOK(statement)  { Acad::ErrorStatus st = (statement); if (st != Acad::eOk)\
  acutPrintf(_T("\nError: Line %ld [%s]\nIn function %s"),\
  __LINE__, acadErrorStatusText(st),_T(__FUNCTION__));}

Code: [Select]
static void ArxAcGiDrawable_undoit(void)
 {
   enableOverrule(Adesk::kFalse);
   MSGNOK(AcRxOverrule::removeOverrule(AcDbLine::desc(),NULL));
 }




Spike Wilbury

  • Guest
Re: Macros for error messages
« Reply #1 on: April 03, 2009, 01:33:49 PM »
Does anybody use macros to make things a little easier checking the status of functions?

Not me.....  :-)

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: Macros for error messages
« Reply #2 on: April 03, 2009, 09:55:25 PM »
Seems pretty neat, I will party with these for a while and see how it works

Code: [Select]
define ISEOKRET(statement)  { Acad::ErrorStatus st = (statement); if (st != Acad::eOk){\
  acutPrintf(_T("\nError: Line %ld [%s]\nIn function %s"),\
  __LINE__, acadErrorStatusText(st),_T(__FUNCTION__)); return st; }}

#define ISEOKMSG(statement)  { Acad::ErrorStatus st = (statement); if (st != Acad::eOk)\
  acutPrintf(_T("\nError: Line %ld [%s]\nIn function %s"),\
  __LINE__, acadErrorStatusText(st),_T(__FUNCTION__));}
« Last Edit: April 04, 2009, 11:13:47 AM by Daniel »

Spike Wilbury

  • Guest
Re: Macros for error messages
« Reply #3 on: April 03, 2009, 10:27:45 PM »
Seems pretty neat, I will party with these for a while and see how it works

Code: [Select]
#define ISEOK(statement)  { Acad::ErrorStatus st = (statement); if (st != Acad::eOk)\
  acutPrintf(_T("\nError: Line %ld [%s]\nIn function %s"),\
  __LINE__, acadErrorStatusText(st),_T(__FUNCTION__)); return st; }

#define ISOKMSG(statement)  { Acad::ErrorStatus st = (statement); if (st != Acad::eOk)\
  acutPrintf(_T("\nError: Line %ld [%s]\nIn function %s"),\
  __LINE__, acadErrorStatusText(st),_T(__FUNCTION__));}

I have seen them, but so far no need of them :-(

Code: [Select]
inline int
EOK(Acad::ErrorStatus arg)
{
return arg == Acad::eOk;
}

Acad::ErrorStatus es;
es = acdbOpenObject(pBlockRef, blkRefIds[i], AcDb::kForWrite);
if (!EOK(es))
break;


inline AcDbDatabase *
CURDB()
{
return acdbHostApplicationServices()->workingDatabase();
}

AcDbBlockTablePointer pBlockTable(CURDB(), AcDb::kForRead);


#define ASSERTREADENABLED \
HRESULT _hr; \
if FAILED(_hr=assertReadEnabled()) \
return _hr;

#define ASSERTWRITEENABLED \
HRESULT _hr; \
if FAILED(_hr=assertWriteEnabled()) \
return _hr;


hmm.... now that I recall, had used them once.... some years ago - if I found more here on my samples will post them.

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: Macros for error messages
« Reply #4 on: April 03, 2009, 10:36:12 PM »
I guess for most, it’s ok just to return an error status, but for noobs like me, I need to be pointed to the exact line in the code where the problem took place. I guess I could use ASSERT, but that’s no fun either  :|

something like this

Code: [Select]
Acad::ErrorStatus
DatabaseTools::AddToModelSpace(AcDbEntity *pEntity, AcDbObjectId &ID, AcDbDatabase *pDb)
{
  if(pEntity == NULL) { ISEOKRET(Acad::eNullEntityPointer); }

  Acad::ErrorStatus es;
  AcDbBlockTable *pBlockTable = NULL;
  AcDbBlockTableRecord  *pBlockTableRecord = NULL;

  es = pDb->getSymbolTable(pBlockTable, AcDb::kForRead);
  ISEOKRET(es);

  es = pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord,AcDb::kForWrite);
  pBlockTable->close();
  ISEOKRET(es);

  es = pBlockTableRecord->appendAcDbEntity(ID, pEntity);
  ISEOKMSG(es);

  es = pEntity->close();
  ISEOKMSG(es);
  es = pBlockTableRecord->close();
  ISEOKMSG(es);

  return es;
}

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: Macros for error messages
« Reply #5 on: April 03, 2009, 10:39:40 PM »
Makes the code cryptic though

MickD

  • Gator
  • Posts: 3309
  • (x-in)->[process]->(y-out)
Re: Macros for error messages
« Reply #6 on: April 03, 2009, 10:40:10 PM »
Macros, while generally avoided in C++ are a very powerful coding tool and can save you quite a lot of hand coding, the only down side is they can make code seem a bit cryptic if you don't know how they work. ARX uses quite a lot of macros too, particularly with custom objects to fill in a lot of the boiler plate code, once you understand that they are simply a preprocessing tool to write code for you automatically rather than some special compiled code they are actually pretty neat. Sort of like code snippets but they fill in the 'blanks' for you.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: To attain knowledge, add things
every day; to obtain wisdom, remove things every day.

MickD

  • Gator
  • Posts: 3309
  • (x-in)->[process]->(y-out)
Re: Macros for error messages
« Reply #7 on: April 03, 2009, 10:40:45 PM »
good timing their Daniel :)
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: To attain knowledge, add things
every day; to obtain wisdom, remove things every day.

Spike Wilbury

  • Guest
Re: Macros for error messages
« Reply #8 on: April 03, 2009, 10:43:37 PM »
the macros looks ugly.... I simple end up using something like this for example:

Code: [Select]
static AcDbObjectId AddEntityToDataBase(AcDbEntity *pEnt, Adesk::UInt16 color = 7)
{
AcDbObjectId pOutputId = AcDbObjectId::kNull;
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecordPointer pBTR(pDb->currentSpaceId(), AcDb::kForWrite);
if (pEnt && Acad::eOk == pBTR.openStatus())
{
pEnt->setDatabaseDefaults();
pEnt->setColorIndex(color);
pBTR->appendAcDbEntity(pOutputId, pEnt);
pEnt->close();
}
return pOutputId;
}

or the well known....
Code: [Select]
bool postToDatabase (AcDbObjectId blkName, AcDbEntity *pEntity)
{
AcDbBlockTableRecordPointer pBlk(blkName, AcDb::kForWrite);
if (pBlk.openStatus() != Acad::eOk) return (false);
return (pBlk->appendAcDbEntity(pEntity) == Acad::eOk);
}

The coding looks cleaner.... IMO

Spike Wilbury

  • Guest
Re: Macros for error messages
« Reply #9 on: April 03, 2009, 10:45:58 PM »
and lately I started to do something like:

Code: [Select]
static void AddEntityToDataBase(AcDbEntity *pEnt, AcDbObjectId &pOutputId, Adesk::UInt16 color = 7)
{
pOutputId = AcDbObjectId::kNull;
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();
AcDbBlockTableRecordPointer pBTR(pDb->currentSpaceId(), AcDb::kForWrite);
if (pEnt && Acad::eOk == pBTR.openStatus())
{
pEnt->setDatabaseDefaults();
pEnt->setColorIndex(color);
pBTR->appendAcDbEntity(pOutputId, pEnt);
pEnt->close();
}
}

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: Macros for error messages
« Reply #10 on: April 03, 2009, 11:01:59 PM »
Im starting on a application thats going to be fairly large, maybe a hundred thousand lines of code or more,
so Im just trying to get a handle on error handling up front. Any advice on this topic is appreciated  :mrgreen:

MP

  • Seagull
  • Posts: 17446
Re: Macros for error messages
« Reply #11 on: April 03, 2009, 11:07:49 PM »
Im starting on a application thats going to be fairly large, maybe a hundred thousand lines of code or more ... any advice on this topic is appreciated :mrgreen:

\|// Set goal. Experiment tirelessly until
|Oo| practice has become expertise.  Loop.
|- | LinkedIn | Dropbox

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: Macros for error messages
« Reply #12 on: April 03, 2009, 11:15:11 PM »
Big time! Espresso with a red bull chaser + a shot of ibuprofen

It's Alive!

  • BricsCAD
  • Needs a day job
  • Posts: 6941
  • AKA Daniel
Re: Macros for error messages
« Reply #13 on: April 03, 2009, 11:33:20 PM »
One side effect of doing this is multiple error messages, .. Stack trace ?




MickD

  • Gator
  • Posts: 3309
  • (x-in)->[process]->(y-out)
Re: Macros for error messages
« Reply #14 on: April 03, 2009, 11:38:04 PM »
I can't find the link I'm looking for but it was something like 'writing effective C' but a quick search failed to locate it, it's more C based but very helpful and covers errors and macros among other things.

Have a look at the ARX macros (go to definition) to see how they do the hard work, basically you write the macro once with 1 or more keywords that are replaced with the keywords used when calling the macro, basically the preprocessor replaces the macro with the code (this may be many lines!) the macro represents in it's place on the fly which saves quite a lot of typing and possible mistakes.
Forth is like the Tao: it is a Way, and is realized when followed.
Its fragility is its strength; its simplicity is its direction - Michael Ham

Lao Tzu: To attain knowledge, add things
every day; to obtain wisdom, remove things every day.