Diche Bach
Diche Bach

Reputation: 45

How to log variable value changes to a CSV file during debug in Visual Studio 2015 C++ Project?

I am working on a war game project using legacy C++ code. Attacks appear to be functioning strangely in "extreme cases" (e.g., a combat in one game "tile" in which 5000 arrows are fired at a group of 3 regiments, one of which can be the target for any of the ~5000 potential "hits" in the course of one game turn). My task is to collate data on what is happening during a game turn.

I need to record how the value for one particular Local variable changes during the course of one game turn for any unit that comes under fire (restricting to a 'test scenario' in which only regiments in one tile are under attack).

My Visual Studio "Watch window" at the beginning of a debug session looks about like this:

Name----------------------Value------------Type
targ-----------------------011-------------short
(gUnit[011]).damage--------0---------------short
(gUnit[047]).damage--------0---------------short
(gUnit[256]).damage--------0---------------short

What this mean is: at the first breakpoint, the regiment represented in nested array gUnit[011]) has been determined to be the target ("targ") for a "hit" and the "damage" for that regiment prior to this hit is = 0.

After one loop that may change to this:

Name----------------------Value------------Type
targ-----------------------256-------------short
(gUnit[011]).damage--------3---------------short
(gUnit[047]).damage--------0---------------short
(gUnit[256]).damage--------0---------------short

Which means: after one "hit" Regiment 011 has suffered 3 damage, and now for the next hit that has been determined to occur Regiment 047 is the target. So for each cycle of the call loop, any of the potential targets can be reassigned as the target and the value of 'damage' can change. For this example of 5000 arrows fired, there could be 5000 hits (though it would be unlikely to be > 1300 hits for that many shots fired). For each hit that occurs in the span of one game turn, I need to record: target and damage

I have breakpoints setup and I can do what I need to do manually meaning:

  1. Advance to first call to the function that determines a hit ["MissileHit()"]

  2. Advance again and note that value for (gUnit[011]).damage has changed from 0 to 3. /* i.e., the Regiment with the ID "011" has taken "3" damage . . . */

  3. Repeat 5000 times for experiment ONE (and noting that, with each loop targ can change to any of the three arrays that represent the units that are under attack in the tile.

  4. Repeat for another 7 experiments (with slightly different conditions) for phase one of testing.

  5. Repeat for perhaps 3 or 4 more phases of testing . . .

In sum, THOUSANDS of rows with at least two columns: targ.......damage

(as a side note, this csv will get additional columns plugged in to it after each test cycle, so the final spreadsheet will look more like this: atkrID....Terrain....atkrExpr....atkrFatig....targID....targDamage

The end result of all this data collection being: descriptive statistics can be calculated on the various in-game variables that mediate the outcomes of attacks)

Obviously, doing this "manually" would be ridiculous.

Is there an easy way to get Visual Studio to generate csv files that will record this data for me?

I can certainly go down the path of writing scripts to run when these breakpoints execute, and fstreaming stuff into a file. But, I'm still wet-behind the ears (both with Visual Studio, C++ and developing in general!) so, I thought it worth asking if there are built in features of Visual Studio that would facilitate this.

Upvotes: 2

Views: 1407

Answers (1)

Jack Zhai
Jack Zhai

Reputation: 6436

I think the VS extension like Drop's comment would be a better path for this issue, but I think it would require the high extension knowledge, so I just provide another workaround for this issue: Add the data breakpoint and output the value to the output windows, even if you also need to copy and past the value to your files, but I think it is also convenient for you to analyze the value during debugging.

Visual Studio. Debug. How to save to a file all the values a variable has had during the duration of a run?

Upvotes: 1

Related Questions