Skip to content

Commit

Permalink
use DebugLog in tpo
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteven4 committed Nov 2, 2024
1 parent 3fb1f2e commit 991c11d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 49 deletions.
2 changes: 1 addition & 1 deletion arcdist.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include "arcdist.h"

#include <cmath> // for round
#include <cstdio> // for printf, sscanf
#include <cstdio> // for sscanf
#include <tuple> // for tie, tuple

#include <QByteArray> // for QByteArray
Expand Down
96 changes: 48 additions & 48 deletions tpo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -327,21 +327,21 @@ int TpoFormatBase::tpo_read_int()

case 0xff: // 32-bit value
if constexpr(debug) {
printf("Found 32-bit value indicator: %x\n", val);
db.log("Found 32-bit value indicator: %x\n", val);
}
return (gbfgetint32(tpo_file_in));
break;

case 0xfe: // 16-bit value
if constexpr(debug) {
printf("Found 16-bit value indicator: %x\n", val);
db.log("Found 16-bit value indicator: %x\n", val);
}
return (gbfgetuint16(tpo_file_in));
break;

default: // 8-bit value
if constexpr(debug) {
printf("Found 8-bit value: %x\n", val);
db.log("Found 8-bit value: %x\n", val);
}
return ((int)val);
break;
Expand Down Expand Up @@ -379,7 +379,7 @@ int TpoFormatBase::tpo_find_block(unsigned int block_desired)
// Read record type
block_type = gbfgetint32(tpo_file_in);
if constexpr(debug) {
printf("Block: %08x\tat offset: %08x\n", block_type, block_offset);
db.log("Block: %08x\tat offset: %08x\n", block_type, block_offset);
}

// Read offset to next record
Expand Down Expand Up @@ -442,20 +442,20 @@ void TpoFormatBase::tpo_process_tracks()
constexpr int debug = 0; // 0-4 for increasingly verbose output in this subroutine)

if constexpr(debug) {
printf("Processing Track Styles... (added in 2012 by SRE)\n");
db.log("Processing Track Styles... (added in 2012 by SRE)\n");
}
// Find block 0x050000 (definitions of styles for free-hand routes)
if (tpo_find_block(0x050000)) {
if constexpr(debug) {
printf("Found no track styles, skipping tracks entirely\n");
db.log("Found no track styles, skipping tracks entirely\n");
}
return;
}
// Read the number of track styles.
unsigned int track_style_count = tpo_read_int(); // 8 bit value

if constexpr(debug) {
printf("Unpacking %u track styles...\n",track_style_count);
db.log("Unpacking %u track styles...\n",track_style_count);
}

QScopedArrayPointer<StyleInfo> styles(new StyleInfo[track_style_count]);
Expand All @@ -467,7 +467,7 @@ void TpoFormatBase::tpo_process_tracks()
unsigned int skipped = (unsigned char) gbfgetc(tpo_file_in);
Q_UNUSED(skipped)
if constexpr(debug > 1) {
printf("Skipping unknown byte 0x%x (? per-zoom-level visibility ?)\n", skipped);
db.log("Skipping unknown byte 0x%x (? per-zoom-level visibility ?)\n", skipped);
}
}

Expand All @@ -484,17 +484,17 @@ void TpoFormatBase::tpo_process_tracks()
unsigned char tmp = gbfgetc(tpo_file_in);
Q_UNUSED(tmp)
if constexpr(debug > 2) {
printf("Skipping unknown byte 0x%x after color (? always zero ?)\n",tmp);
db.log("Skipping unknown byte 0x%x after color (? always zero ?)\n",tmp);
}

// byte for track style name length, then name itself
tmp = gbfgetc(tpo_file_in);
// wrong byte order?? tmp = tpo_read_int(); // 16 bit value
if constexpr(debug > 1) {
printf("Track style %u has %d-byte (0x%x) name\n", ii, tmp, tmp);
db.log("Track style %u has %d-byte (0x%x) name\n", ii, tmp, tmp);
}
if (tmp >= TRACKNAMELENGTH) {
printf("ERROR! Found track style name over %d chars, skipping all tracks!\n",TRACKNAMELENGTH);
warning("ERROR! Found track style name over %d chars, skipping all tracks!\n",TRACKNAMELENGTH);
return;
}
if (tmp) {
Expand Down Expand Up @@ -526,24 +526,24 @@ void TpoFormatBase::tpo_process_tracks()
for (unsigned xx = 0; xx < 2; xx++) {
tmp = gbfgetc(tpo_file_in);
if constexpr(debug > 2) {
printf("Skipping trailing line style byte 0x%x (? always zero ?)\n", tmp);
db.log("Skipping trailing line style byte 0x%x (? always zero ?)\n", tmp);
}
}

if constexpr(debug) {
printf("Track style %u: color=#%02x%02x%02x, width=%d, dashed=%d, name=%s\n",
db.log("Track style %u: color=#%02x%02x%02x, width=%d, dashed=%d, name=%s\n",
ii, styles[ii].color[0], styles[ii].color[1], styles[ii].color[2], styles[ii].wide, styles[ii].dash, qPrintable(styles[ii].name));
}
}

if constexpr(debug) {
printf("Done Processing Track Styles... found %u styles\n", track_style_count);
db.log("Done Processing Track Styles... found %u styles\n", track_style_count);
}

// Find block 0x060000 (free-hand routes) (original track code, pre-2012, without styles)
if (tpo_find_block(0x060000)) {
if constexpr(debug) {
printf("Found no track data block, skipping all tracks!\n");
db.log("Found no track data block, skipping all tracks!\n");
}
return;
}
Expand All @@ -552,12 +552,12 @@ void TpoFormatBase::tpo_process_tracks()
unsigned int track_count = tpo_read_int();

if constexpr(debug) {
printf("Number of tracks in file: %u\n", track_count);
db.log("Number of tracks in file: %u\n", track_count);
}

if (track_count == 0) {
if constexpr(debug) {
printf("Found no track data, even though there was a track data block!\n");
db.log("Found no track data, even though there was a track data block!\n");
}
return;
}
Expand All @@ -566,7 +566,7 @@ void TpoFormatBase::tpo_process_tracks()
//
for (unsigned ii = 0; ii < track_count; ii++) {
if constexpr(debug > 1) {
printf("\nStarting Track %u",ii+1);
db.log("\nStarting Track %u",ii+1);
}
int lat = 0;
int lon = 0;
Expand All @@ -591,13 +591,13 @@ void TpoFormatBase::tpo_process_tracks()
if (name_length) {
gbfread(track_name, 1, name_length, tpo_file_in);
if constexpr(debug > 2) {
printf(", length %.0fm?, named %s\n", track_length, qPrintable(track_name));
db.log(", length %.0fm?, named %s\n", track_length, qPrintable(track_name));
}
} else { // Assign a generic track name
track_name = "TRK ";
track_name += QString::number(ii + 1);
if constexpr(debug > 2) {
printf(", length %.0fm?, inventing name %s\n", track_length, qPrintable(track_name));
db.log(", length %.0fm?, inventing name %s\n", track_length, qPrintable(track_name));
}
}
track_temp->rte_name = track_name;
Expand All @@ -623,7 +623,7 @@ void TpoFormatBase::tpo_process_tracks()
track_temp->line_width = styles[track_style].wide;

if constexpr(debug) {
printf("Track Name: %s, ?Type?: %u, Style Name: %s, Width: %d, Dashed: %d, Color: #%s\n",
db.log("Track Name: %s, ?Type?: %u, Style Name: %s, Width: %d, Dashed: %d, Color: #%s\n",
qPrintable(track_name), line_type,
qPrintable(styles[track_style].name),
styles[track_style].wide, styles[track_style].dash,
Expand Down Expand Up @@ -725,21 +725,21 @@ void TpoFormatBase::tpo_process_tracks()
Waypoint* waypoint_temp;
#ifdef Tracks2012
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - byte %u, track %u, llvallid=%d\n",
db.log("%02x %02x %02x %02x - byte %u, track %u, llvallid=%d\n",
buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], jj, ii+1, llvalid);
}
// Time to read a new latlong?
if (!llvalid) {

lon = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lon,jj);
db.log("%02x %02x %02x %02x - raw lon = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lon,jj);
}
jj+=4;

lat = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lat,jj);
db.log("%02x %02x %02x %02x - raw lat = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lat,jj);
}
jj+=4;

Expand All @@ -755,7 +755,7 @@ void TpoFormatBase::tpo_process_tracks()

lonscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lonscale, jj);
db.log("%02x %02x %02x %02x - raw lon scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lonscale, jj);
}
//printf(" LONSCALE:");
//printf("%02x%02x%02x%02x", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3]);
Expand All @@ -771,7 +771,7 @@ void TpoFormatBase::tpo_process_tracks()

latscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], latscale, jj);
db.log("%02x %02x %02x %02x - raw lat scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], latscale, jj);
}
//printf(" LATSCALE:");
//printf("%02x%02x%02x%02x ", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3]);
Expand All @@ -783,40 +783,40 @@ void TpoFormatBase::tpo_process_tracks()
track_add_wpt(track_temp, waypoint_temp);
cnttp++;
if constexpr(debug > 3) {
printf("Adding BASIC trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
db.log("Adding BASIC trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
}
}
#else
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x = bytes %u-%u (track %u, mode now %s)\n",
db.log("%02x %02x %02x %02x = bytes %u-%u (track %u, mode now %s)\n",
buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], jj, jj+3, ii+1, tpmodeshow[tpmode]);
}

// read 8-byte lon+lat, required at start of track or after 0x88 tag
if (tpmode == GetFullPoint) {
lon = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lon,jj);
db.log("%02x %02x %02x %02x - raw lon = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lon,jj);
}
jj+=4;

lat = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lat,jj);
db.log("%02x %02x %02x %02x - raw lat = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lat,jj);
}
jj+=4;

waypoint_temp = tpo_convert_ll(lat, lon);
track_add_wpt(track_temp, waypoint_temp);
cnttp++;
if (((abs(waypoint_temp->latitude - lastlat) > 1.0) && lastlat) || ((abs(waypoint_temp->longitude - lastlon) > 1.0) && lastlon)) {
printf("WARNING! Track '%s' point #%d is more than 1 degree from the last track point!\n (probably corrupt - try splitting in two at sharp corners)\n", qPrintable(track_name), cnttp);
warning("WARNING! Track '%s' point #%d is more than 1 degree from the last track point!\n (probably corrupt - try splitting in two at sharp corners)\n", qPrintable(track_name), cnttp);
}
lastlat = waypoint_temp->latitude;
lastlon = waypoint_temp->longitude;

if constexpr(debug > 3) {
printf("Adding BASIC trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
db.log("Adding BASIC trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
}

// after full point, can have scaling or 0x88 for another full point or single byte to be scaled
Expand All @@ -835,7 +835,7 @@ void TpoFormatBase::tpo_process_tracks()
if ((jj+3<track_byte_count) && !(buf[jj+3]) && !(buf[jj+2])) {
lonscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lon scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lonscale, jj);
db.log("%02x %02x %02x %02x - raw lon scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], lonscale, jj);
}
jj+=4;
tpmode = CheckLatScale;
Expand All @@ -854,7 +854,7 @@ void TpoFormatBase::tpo_process_tracks()
if ((jj+3<track_byte_count) && !(buf[jj+3]) && !(buf[jj+2])) {
latscale = le_read32(&buf[jj]);
if constexpr(debug > 3) {
printf("%02x %02x %02x %02x - raw lat scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], latscale, jj);
db.log("%02x %02x %02x %02x - raw lat scale = %d (byte %u)\n", buf[jj], buf[jj+1], buf[jj+2], buf[jj+3], latscale, jj);
}
jj+=4;
}
Expand All @@ -870,7 +870,7 @@ void TpoFormatBase::tpo_process_tracks()
// offsets.
else if (buf[jj] == 0x88) {
if constexpr(debug > 3) {
printf("%02x should mean full lat/lon comes next (byte %u)\n",buf[jj],jj);
db.log("%02x should mean full lat/lon comes next (byte %u)\n",buf[jj],jj);
}
jj++;
llvalid = 0;
Expand All @@ -881,7 +881,7 @@ void TpoFormatBase::tpo_process_tracks()
if (tpmode == Check0x88Tag) {
if (buf[jj] == FullPointTag) {
if constexpr(debug > 3) {
printf("%02x should mean full lat/lon comes next (byte %u)\n",buf[jj],jj);
db.log("%02x should mean full lat/lon comes next (byte %u)\n",buf[jj],jj);
}
jj++;
tpmode = GetFullPoint;
Expand All @@ -897,7 +897,7 @@ void TpoFormatBase::tpo_process_tracks()
// combo embedded in this track next.
else if (buf[jj] == 0x00) {
if constexpr(debug > 3) {
printf("%02x should mean full lat/lon or lonscale/latscale comes next (at byte %u)\n",buf[jj],jj);
db.log("%02x should mean full lat/lon or lonscale/latscale comes next (at byte %u)\n",buf[jj],jj);
}
//printf(" ZERO ");
jj++;
Expand All @@ -908,7 +908,7 @@ void TpoFormatBase::tpo_process_tracks()
else {
static const int scarray[] = {0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1};
if constexpr(debug) {
printf("%02x - lat mult = %d, lon mult=%d, byte %u\n", buf[jj], scarray[buf[jj] & 0xf], scarray[buf[jj] >> 4], jj);
db.log("%02x - lat mult = %d, lon mult=%d, byte %u\n", buf[jj], scarray[buf[jj] & 0xf], scarray[buf[jj] >> 4], jj);
}


Expand All @@ -922,12 +922,12 @@ void TpoFormatBase::tpo_process_tracks()


if constexpr(debug > 3) {
printf("%02x - adjusting prev lat/lon from %i/%i", buf[jj], lat, lon);
db.log("%02x - adjusting prev lat/lon from %i/%i", buf[jj], lat, lon);
}
lon += lonscale * scarray[buf[jj] >> 4];
lat += latscale * scarray[(buf[jj] & 0xf)];
if constexpr(debug > 3) {
printf(" to %i/%i, byte %u\n", lat, lon, jj);
db.log(" to %i/%i, byte %u\n", lat, lon, jj);
}
//printf(".");
jj++;
Expand All @@ -936,7 +936,7 @@ void TpoFormatBase::tpo_process_tracks()
track_add_wpt(track_temp, waypoint_temp);
cnttp++;
if constexpr(debug > 3) {
printf("Adding ADJUSTED trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
db.log("Adding ADJUSTED trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
}
}
#else
Expand All @@ -947,7 +947,7 @@ void TpoFormatBase::tpo_process_tracks()
// list of single bytes to be scaled can only end with 0x00, can then have full point or scaling
if (buf[jj] == EndScaleTag) {
if constexpr(debug > 3) {
printf("%02x should mean full lat/lon or lonscale/latscale comes next (at byte %u)\n",buf[jj],jj);
db.log("%02x should mean full lat/lon or lonscale/latscale comes next (at byte %u)\n",buf[jj],jj);
}
jj++;
tpmode = GetFullPoint;
Expand All @@ -959,7 +959,7 @@ void TpoFormatBase::tpo_process_tracks()

if (buf[jj] == FullPointTag) {
if constexpr(debug > 3) {
printf("%02x should mean full lat/lon comes next (at byte %u)\n",buf[jj],jj);
db.log("%02x should mean full lat/lon comes next (at byte %u)\n",buf[jj],jj);
}
jj++;
tpmode = GetFullPoint;
Expand All @@ -970,7 +970,7 @@ void TpoFormatBase::tpo_process_tracks()
static const int scarray[] = {0,1,2,3,4,5,6,7,-8,-7,-6,-5,-4,-3,-2,-1}; // MAGIC! (no idea where this comes from)

if constexpr(debug) {
printf("%02x - lat mult = %d, lon mult=%d, byte %u\n", buf[jj], scarray[buf[jj] & 0xf], scarray[buf[jj] >> 4], jj);
db.log("%02x - lat mult = %d, lon mult=%d, byte %u\n", buf[jj], scarray[buf[jj] & 0xf], scarray[buf[jj] >> 4], jj);
}
if (buf[jj] == 0) {
fatal("Found unexpected ZERO\n");
Expand All @@ -981,24 +981,24 @@ void TpoFormatBase::tpo_process_tracks()
}

if constexpr(debug > 3) {
printf("%02x - adjusting prev lat/lon from %i/%i", buf[jj], lat, lon);
db.log("%02x - adjusting prev lat/lon from %i/%i", buf[jj], lat, lon);
}
lon += lonscale * scarray[buf[jj] >> 4];
lat += latscale * scarray[(buf[jj] & 0xf)];
if constexpr(debug > 3) {
printf(" to %i/%i, byte %u\n", lat, lon, jj);
db.log(" to %i/%i, byte %u\n", lat, lon, jj);
}
jj++;

waypoint_temp = tpo_convert_ll(lat, lon);
track_add_wpt(track_temp, waypoint_temp);
cnttp++;
if constexpr(debug > 3) {
printf("Adding ADJUSTED trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
db.log("Adding ADJUSTED trackpoint #%i: lat=%.5f, lon=%.5f\n", cnttp, waypoint_temp->latitude, waypoint_temp->longitude);
}

if (((abs(waypoint_temp->latitude - lastlat) > 1) && lastlat) || ((abs(waypoint_temp->longitude - lastlon) > 1) && lastlon)) {
printf("WARNING! Track '%s' point #%i is more than 1 degree from the last track point!\n (probably corrupt - try splitting in two at sharp corners)\n", qPrintable(track_name), cnttp);
warning("WARNING! Track '%s' point #%i is more than 1 degree from the last track point!\n (probably corrupt - try splitting in two at sharp corners)\n", qPrintable(track_name), cnttp);
}
lastlat = waypoint_temp->latitude;
lastlon = waypoint_temp->longitude;
Expand Down
Loading

0 comments on commit 991c11d

Please sign in to comment.