0

I have a large C++ code base that contains a couple of functions for error logging that I'm planning to rewrite, defined as follows;

void LogError(ErrorLevel elvl,LPCTSTR Format,...);  // Literal version
void LogError(ErrorLevel elvl,UINT ResourceID,...); // Resource version

I'm planning to rewrite these as a single function

void LogError(ErrNo No,...);

ErrNo in this case is will be an enum, used to look up the rest of the error details from an external file. While I'm using and love Visual Assist, it doesn't appear to be up to this kind of thing. I'm thinking the easiest way to carry out this refactor is to write a small program that uses the results of a search output to find all the occurences of this function, e.g.

    c:\cpp\common\Atlas\Comps\LSADJUST.cpp
        LSAFormNormalEquations (174):    LogError(elvl_Error,IDS_WINWRN0058,i+1,TravObs.setup_no,TravObs.round_no
        LSAFormNormalEquations (180):    LogError(elvl_Error,IDS_WINWRN0059,i+1,TravObs.setup_no,TravObs.round_no
        LSAFormNormalEquations (186):    LogError(elvl_Error,IDS_WINWRN0060,i+1,TravObs.setup_no,TravObs.round_no
    c:\cpp\common\Atlas\Comps\LSADJUSTZ.CPP
        LSAFormNormalEquationsZ (45):    LogError(elvl_Note,_T("Adjusting heights by least squares"));
    c:\cpp\Win32\Atlas\Section\OptmizeSectionVolumes.cpp
        OnSectionOptimizeVolumes (239):    LogError(elvl_Note,"Shifted section at chainage %0.1lf by %0.3lf",Graph.c1,Offset);

and then parse and modify the source. Are there any other tools that could simplify this task for me? If looked at a related question which suggets there isn't much out there. I don't mind spending a small amount for a reasonably easy to use tool, but don't have the time or budget for anything more than this.

Community
  • 1
  • 1
SmacL
  • 22,555
  • 12
  • 95
  • 149
  • 2
    How many hits does a search come up with? In such cases I often found global search plus a VS macro quite helpful already. – sbi Jan 17 '11 at 13:41
  • @sbi - 1217 hits. Just about enough to warrant writing a bit of code rather than doing it manually. Particularly given my short attention span for doing this type of work manually ;) – SmacL Jan 17 '11 at 13:57
  • If you have an output window listing all of them, you can step from one to the other using a shortcut (`F4` for me), and start a VS macro using another (`Ctrl+Shift+P` for me, and you might be able to incorporate the `F4` one into the macro). That's pretty straightforward, I'd think. I suppose I have done worse replacements than this one. – sbi Jan 17 '11 at 14:20
  • I've done stuff like this with Codewright's regular expression search and replace. Back up regularly though, because it is easy to destroy the whole code set with a single typo :-) – Tony Park Jan 17 '11 at 14:28
  • @Tony: I wouldn't dare to do something like this without the backup of a VCS. – sbi Jan 17 '11 at 14:52

2 Answers2

1

If you were using Unix, using sed to edit all your source-code might handle most of the changes. You would have to complete some of the changes by hand. I have used this technique in the past.

Raedwald
  • 46,613
  • 43
  • 151
  • 237
  • You can use cygwin or MSYS for that on Windows. – sbi Jan 17 '11 at 13:43
  • @sbi yes, but a purely Windows programmer might not know how to use them. Installing cygwin and learning how to use several Unix programs to perform one refactoring is a tad too much. – Raedwald Jan 17 '11 at 14:13
  • That's indeed true. Often, however, you might be able to ask a co-worker. That's what I always did. `:)` – sbi Jan 17 '11 at 14:14
  • Or powershell, which is free and comes as default in Windows7. – tenpn Jan 28 '11 at 08:52
0

Searching around for something light weight that met my needs drew a blank, and learning SED while worthwhile would have been a fair amount of work for something that did not quite solve my problem. I ended up writing my own tool to carry out the refactor needed on a seperate copy of the code base until I was happy that it was doing exactly what I needed. This involved taking the output from Visual Assists find all references option, and use it to refactor the code base. I'd post the code, but as it stands is pretty awful and would be liable to fail under a different code base. The general problem can be better stated as something like this

  • For a C++ code base, find every occurence of function fn taking parameters a,b,...n
  • Remove the occurence of fn from the source file
  • Extract the parameters as text variables
  • Add a few more variables such as instance number, source file name, etc...
  • At the point where fn was removed, write a formatted string that can include available variables
  • Append similar formatted strings to one or more external files (e.g. resource files etc...)

I'm guessing the above functionality would be easy enough to implement for someone who is already parsing the source, and will pass a link to this question to Whole Tomato as an enhancement suggestion.

Edit: For anyone interested, there's some follow up on the VA forum here.

SmacL
  • 22,555
  • 12
  • 95
  • 149