Move custom libdxfrw logging class to QGIS code, out of copy of libdxfrw

This commit is contained in:
Nyall Dawson 2021-08-13 15:46:22 +10:00
parent 74b0cfd8ec
commit ec23c24e18
2 changed files with 111 additions and 53 deletions

View File

@ -10,17 +10,14 @@
** along with this program. If not, see <http://www.gnu.org/licenses/>. **
******************************************************************************/
#include <iostream>
#include <iomanip>
#include "drw_dbg.h"
#include "qgslogger.h"
DRW_dbg *DRW_dbg::instance{nullptr};
/*********private clases*************/
#include <QTextStream>
#include <QStringList>
DRW_dbg *DRW_dbg::instance = nullptr;
/*********private classes*************/
class print_debug : public DRW::DebugPrinter {
public:
void printS(const std::string& s) override;
@ -31,12 +28,8 @@ public:
void printB(int i) override;
void printHL(int c, int s, int h) override;
void printPT(double x, double y, double z) override;
~print_debug() override { QgsDebugMsgLevel( mBuf, 5 ); }
private:
std::ios_base::fmtflags flags{std::cerr.flags()};
QString mBuf;
QTextStream mTS;
void flush();
};
/********* debug class *************/
@ -119,60 +112,41 @@ void DRW_dbg::printPT(double x, double y, double z){
currentPrinter->printPT(x, y, z);
}
void print_debug::flush()
{
QStringList lines = mBuf.split( '\n' );
for ( int i = 0; i < lines.size() - 1; i++ )
{
QgsDebugMsgLevel( lines[i], 4 );
}
mBuf = lines.last();
void print_debug::printS(const std::string& s){
std::cerr << s;
}
void print_debug::printS( const std::string& s )
{
mTS << QString::fromStdString( s );
flush();
void print_debug::printI(long long int i){
std::cerr << i;
}
void print_debug::printI( long long int i )
{
mTS << i;
flush();
void print_debug::printUI(long long unsigned int i){
std::cerr << i;
}
void print_debug::printUI( long long unsigned int i )
{
mTS << i;
flush();
void print_debug::printD(double d){
std::cerr << std::fixed << d;
}
void print_debug::printD( double d )
{
mTS << QStringLiteral( "%1 " ).arg( d, 0, 'g' );
flush();
void print_debug::printH(long long i){
std::cerr << "0x" << std::setw(2) << std::setfill('0');
std::cerr << std::hex << i;
std::cerr.flags(flags);
}
void print_debug::printH( long long i )
{
mTS << QStringLiteral( "0x%1" ).arg( i, 0, 16 );
flush();
void print_debug::printB(int i){
std::cerr << std::setw(8) << std::setfill('0');
std::cerr << std::setbase(2) << i;
std::cerr.flags(flags);
}
void print_debug::printB( int i )
{
mTS << QStringLiteral( "0%1" ).arg( i, 0, 8 );
flush();
void print_debug::printHL(int c, int s, int h){
std::cerr << c << '.' << s << '.';
std::cerr << "0x" << std::setw(2) << std::setfill('0');
std::cerr << std::hex << h;
std::cerr.flags(flags);
}
void print_debug::printHL( int c, int s, int h )
{
mTS << QStringLiteral( "%1.%2 0x%3" ).arg( c ).arg( s ).arg( h, 0, 16 );
flush();
}
void print_debug::printPT( double x, double y, double z )
{
mTS << QStringLiteral( "x:%1 y:%2 z:%3" ).arg( x, 0, 'g' ).arg( y, 0, 'g' ).arg( z, 0, 'g' );
flush();
void print_debug::printPT(double x, double y, double z){
std::cerr << std::fixed << "x: " << x << ", y: " << y << ", z: "<< z;
}

View File

@ -48,6 +48,7 @@
#include <gdal.h>
#include <ogr_srs_api.h>
#include <memory>
#include <mutex>
#define LOG( x ) { QgsDebugMsg( x ); QgsMessageLog::logMessage( x, QObject::tr( "DWG/DXF import" ) ); }
#define ONCE( x ) { static bool show=true; if( show ) LOG( x ); show=false; }
@ -64,6 +65,82 @@
#endif
class QgsDrwDebugPrinter : public DRW::DebugPrinter
{
public:
QgsDrwDebugPrinter()
: mTS( &mBuf )
{ }
~QgsDrwDebugPrinter() override
{
QgsDebugMsgLevel( mBuf, 4 );
}
void printS( const std::string &s ) override
{
mTS << QString::fromStdString( s );
flush();
}
void printI( long long int i ) override
{
mTS << i;
flush();
}
void printUI( long long unsigned int i ) override
{
mTS << i;
flush();
}
void printD( double d ) override
{
mTS << QStringLiteral( "%1 " ).arg( d, 0, 'g' );
flush();
}
void printH( long long int i ) override
{
mTS << QStringLiteral( "0x%1" ).arg( i, 0, 16 );
flush();
}
void printB( int i ) override
{
mTS << QStringLiteral( "0%1" ).arg( i, 0, 8 );
flush();
}
void printHL( int c, int s, int h ) override
{
mTS << QStringLiteral( "%1.%2 0x%3" ).arg( c ).arg( s ).arg( h, 0, 16 );
flush();
}
void printPT( double x, double y, double z ) override
{
mTS << QStringLiteral( "x:%1 y:%2 z:%3" ).arg( x, 0, 'g' ).arg( y, 0, 'g' ).arg( z, 0, 'g' );
flush();
}
private:
std::ios_base::fmtflags flags{std::cerr.flags()};
QString mBuf;
QTextStream mTS;
void flush()
{
QStringList lines = mBuf.split( '\n' );
for ( int i = 0; i < lines.size() - 1; i++ )
{
QgsDebugMsgLevel( lines[i], 4 );
}
mBuf = lines.last();
}
};
QgsDwgImporter::QgsDwgImporter( const QString &database, const QgsCoordinateReferenceSystem &crs )
: mDs( nullptr )
, mDatabase( database )
@ -76,6 +153,13 @@ QgsDwgImporter::QgsDwgImporter( const QString &database, const QgsCoordinateRefe
{
QgsDebugCall;
// setup custom debug printer for libdxfrw
static std::once_flag initialized;
std::call_once( initialized, [ = ]( )
{
DRW::setCustomDebugPrinter( new QgsDrwDebugPrinter() );
} );
const QString crswkt( crs.toWkt( QgsCoordinateReferenceSystem::WKT_PREFERRED_GDAL ) );
mCrsH = QgsOgrUtils::crsToOGRSpatialReference( crs );
QgsDebugMsg( QStringLiteral( "CRS %1[%2]: %3" ).arg( mCrs ).arg( ( qint64 ) mCrsH, 0, 16 ).arg( crswkt ) );