From bd82f0cc2e0fd62fe349bcacdd17cd0e371e73b2 Mon Sep 17 00:00:00 2001 From: Vijay Nayar Date: Sat, 7 Jan 2023 10:45:48 +0100 Subject: [PATCH] Fixes #112: Migrate to std.logger (no longer experimental since DMD 2.101.0) (#113) * Fixes #112: Migrate from std.experimental.logger (incompatible since DMD 2.101.0) to std.logger. - Removed explicit references to `sharedLogger`, which are now explicitly of `shared` type. Replaced with calls to base function, e.g. `warnf`, which routes to the default logger. See https://dlang.org/changelog/2.101.0.html#logger_sharedLog_returning_shared_logger - Replaced imports of `std.experimental.logger` with `std.logger`, because `std.experimental.logger` has been deprecated since DMD 2.101.0. * Make dependency on std.logger conditional, fall back to std.experimental.logger. * Added logger to integration tests. --- example/source/testddbc.d | 7 ++++-- source/ddbc/common.d | 20 +++++++---------- source/ddbc/drivers/mysqlddbc.d | 18 ++++++--------- source/ddbc/drivers/odbcddbc.d | 38 ++++++++++++-------------------- source/ddbc/drivers/pgsqlddbc.d | 18 ++++++--------- source/ddbc/drivers/sqliteddbc.d | 20 +++++++---------- source/ddbc/drivers/utils.d | 10 ++------- source/ddbc/pods.d | 26 +++++++--------------- test/ddbctest/common.d | 18 +++++++++------ test/ddbctest/main.d | 2 ++ 10 files changed, 72 insertions(+), 105 deletions(-) diff --git a/example/source/testddbc.d b/example/source/testddbc.d index bbdbeb7..3e49846 100644 --- a/example/source/testddbc.d +++ b/example/source/testddbc.d @@ -86,11 +86,14 @@ struct ConnectionParams } int main(string[] args) { - static if(__traits(compiles, (){ import std.experimental.logger; } )) { + static if (__traits(compiles, (){ import std.logger; } )) { + import std.logger; + } else { import std.experimental.logger; - globalLogLevel(LogLevel.all); } + globalLogLevel(LogLevel.all); + ConnectionParams par; string URI; Driver driver; diff --git a/source/ddbc/common.d b/source/ddbc/common.d index c15df4b..a622140 100644 --- a/source/ddbc/common.d +++ b/source/ddbc/common.d @@ -24,10 +24,12 @@ module ddbc.common; import ddbc.core; import std.algorithm; import std.exception; -static if(__traits(compiles, (){ import std.experimental.logger; } )) { +static if (__traits(compiles, (){ import std.logger; } )) { + import std.logger; +} else { import std.experimental.logger; - pragma(msg, "DDBC will log using 'std.experimental.logger'."); } + import std.stdio; import std.conv; import std.variant; @@ -162,9 +164,7 @@ public: Connection conn = null; //writeln("getConnection(): freeConnections.length = " ~ to!string(freeConnections.length)); if (freeConnections.length > 0) { - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.tracef("Retrieving database connection from pool of %s", freeConnections.length); - } + tracef("Retrieving database connection from pool of %s", freeConnections.length); conn = freeConnections[freeConnections.length - 1]; // $ - 1 auto oldSize = freeConnections.length; myRemove(freeConnections, freeConnections.length - 1); @@ -172,14 +172,12 @@ public: auto newSize = freeConnections.length; assert(newSize == oldSize - 1); } else { - sharedLog.tracef("Creating new database connection (%s) %s %s", driver, url, params); + tracef("Creating new database connection (%s) %s %s", driver, url, params); try { conn = super.getConnection(); } catch (Throwable e) { - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.errorf("could not create db connection : %s", e.msg); - } + errorf("could not create db connection : %s", e.msg); throw e; } //writeln("getConnection(): connection created"); @@ -201,9 +199,7 @@ public: //activeConnections.length = oldSize - 1; auto newSize = activeConnections.length; assert(oldSize == newSize + 1); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.tracef("database connections reduced from %s to %s", oldSize, newSize); - } + tracef("database connections reduced from %s to %s", oldSize, newSize); return; } } diff --git a/source/ddbc/drivers/mysqlddbc.d b/source/ddbc/drivers/mysqlddbc.d index ceef947..e6dd970 100644 --- a/source/ddbc/drivers/mysqlddbc.d +++ b/source/ddbc/drivers/mysqlddbc.d @@ -30,10 +30,12 @@ import std.datetime : Date, DateTime, TimeOfDay; import std.datetime.date; import std.datetime.systime; import std.exception : enforce; - -static if(__traits(compiles, (){ import std.experimental.logger; } )) { +static if (__traits(compiles, (){ import std.logger; } )) { + import std.logger; +} else { import std.experimental.logger; } + import std.stdio; import std.string; import std.variant; @@ -361,9 +363,7 @@ public: lock(); scope(exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(queryString); - } + trace(queryString); try { results = query(conn.getConnection(), queryString); @@ -379,9 +379,7 @@ public: scope(exit) unlock(); ulong rowsAffected = 0; - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(query); - } + trace(query); try { rowsAffected = exec(conn.getConnection(), query); @@ -511,9 +509,7 @@ public: lock(); scope(exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(statement.sql()); - } + trace(statement.sql()); try { results = query(conn.getConnection(), statement); diff --git a/source/ddbc/drivers/odbcddbc.d b/source/ddbc/drivers/odbcddbc.d index b37af4a..afc40e0 100644 --- a/source/ddbc/drivers/odbcddbc.d +++ b/source/ddbc/drivers/odbcddbc.d @@ -27,10 +27,12 @@ import std.datetime : Date, DateTime, TimeOfDay; import std.datetime.date; import std.datetime.systime; import std.exception : enforce; - -static if(__traits(compiles, (){ import std.experimental.logger; } )) { +static if (__traits(compiles, (){ import std.logger; } )) { + import std.logger; +} else { import std.experimental.logger; } + import std.stdio; import std.string; import std.variant; @@ -105,9 +107,9 @@ version (USE_ODBC) debug { if(retval < 0) { - sharedLog.errorf("%s(%s) : %s", fullyQualifiedName!fn, format("%(%s%|, %)", tuple(args)), cast(RetVals) retval); + errorf("%s(%s) : %s", fullyQualifiedName!fn, format("%(%s%|, %)", tuple(args)), cast(RetVals) retval); } else { - //sharedLog.tracef("%s(%s) : %s", fullyQualifiedName!fn, format("%(%s%|, %)", tuple(args)), cast(RetVals) retval); + //tracef("%s(%s) : %s", fullyQualifiedName!fn, format("%(%s%|, %)", tuple(args)), cast(RetVals) retval); } } @@ -423,7 +425,7 @@ version (USE_ODBC) addToConnectionString("trusted_connection", "TrustServerCertificate"); string connectionString = connectionProps.join(';'); - sharedLog.info(connectionString); + info(connectionString); SQLCHAR[1024] outstr; SQLSMALLINT outstrlen; @@ -622,9 +624,7 @@ version (USE_ODBC) scope (exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(query); - } + trace(query); try { @@ -657,9 +657,7 @@ version (USE_ODBC) unlock(); int rowsAffected = 0; - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(query); - } + trace(query); try { @@ -682,9 +680,7 @@ version (USE_ODBC) scope (exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(query); - } + trace(query); try { @@ -1011,9 +1007,7 @@ version (USE_ODBC) scope (exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(stmt); - } + trace(stmt); try { @@ -1037,9 +1031,7 @@ version (USE_ODBC) scope (exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(stmt); - } + trace(stmt); try { @@ -1069,9 +1061,7 @@ version (USE_ODBC) scope (exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(stmt); - } + trace(stmt); try { @@ -1340,7 +1330,7 @@ version (USE_ODBC) items[i].typeName = (cast(SqlType) items[i].type).to!(string); items[i].isNullable = col.nullAble == SQL_NULLABLE; - debug sharedLog.tracef("Column meta data: catalogName='%s', name='%s', typeName='%s'", items[i].catalogName, items[i].name, items[i].typeName); + debug tracef("Column meta data: catalogName='%s', name='%s', typeName='%s'", items[i].catalogName, items[i].name, items[i].typeName); } metadata = new ResultSetMetaDataImpl(items); diff --git a/source/ddbc/drivers/pgsqlddbc.d b/source/ddbc/drivers/pgsqlddbc.d index 1c618b0..e8324fe 100644 --- a/source/ddbc/drivers/pgsqlddbc.d +++ b/source/ddbc/drivers/pgsqlddbc.d @@ -32,8 +32,9 @@ version(USE_PGSQL) { import std.datetime.date; import std.datetime.systime; import std.exception : enforce; - - static if(__traits(compiles, (){ import std.experimental.logger; } )) { + static if (__traits(compiles, (){ import std.logger; } )) { + import std.logger; + } else { import std.experimental.logger; } import std.stdio; @@ -578,9 +579,8 @@ version(USE_PGSQL) { lock(); scope(exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(query); - } + trace(query); + PGresult * res = PQexec(conn.getConnection(), std.string.toStringz(query)); enforce!SQLException(res !is null, "Failed to execute statement " ~ query); auto status = PQresultStatus(res); @@ -624,9 +624,7 @@ version(USE_PGSQL) { lock(); scope(exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(query); - } + trace(query); PGresult * res = PQexec(conn.getConnection(), std.string.toStringz(query)); enforce!SQLException(res !is null, "Failed to execute statement " ~ query); @@ -835,9 +833,7 @@ version(USE_PGSQL) { lock(); scope(exit) unlock(); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(this.query); - } + trace(this.query); PGresult * res = exec(); scope(exit) PQclear(res); diff --git a/source/ddbc/drivers/sqliteddbc.d b/source/ddbc/drivers/sqliteddbc.d index d10e5c0..25847ee 100644 --- a/source/ddbc/drivers/sqliteddbc.d +++ b/source/ddbc/drivers/sqliteddbc.d @@ -31,10 +31,12 @@ version(USE_SQLITE) { import std.datetime.systime : SysTime, Clock; import std.datetime.timezone : UTC; import std.exception : enforce; - - static if(__traits(compiles, (){ import std.experimental.logger; } )) { + static if (__traits(compiles, (){ import std.logger; } )) { + import std.logger; + } else { import std.experimental.logger; } + import std.stdio; import std.string; import std.variant; @@ -171,10 +173,8 @@ version(USE_SQLITE) { // YYYY-MM-DD HH:MM:SS.SSS // YYYY-MM-DDTHH:MM:SS // YYYY-MM-DDTHH:MM:SS.SSS - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - if(sqliteString.length > 19) { - sharedLog.warning(sqliteString ~ " will be converted to DateTime and lose the milliseconds. Consider using SysTime"); - } + if(sqliteString.length > 19) { + warning(sqliteString ~ " will be converted to DateTime and lose the milliseconds. Consider using SysTime"); } auto date = Date.fromISOExtString(sqliteString[0..10]); @@ -442,9 +442,7 @@ version(USE_SQLITE) { override ddbc.core.ResultSet executeQuery(string query) { closePreparedStatement(); _currentStatement = conn.prepareStatement(query); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(_currentStatement); - } + trace(_currentStatement); _currentResultSet = _currentStatement.executeQuery(); return _currentResultSet; } @@ -462,9 +460,7 @@ version(USE_SQLITE) { closePreparedStatement(); _currentStatement = conn.prepareStatement(query); - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - sharedLog.trace(_currentStatement); - } + trace(_currentStatement); return _currentStatement.executeUpdate(insertId); } diff --git a/source/ddbc/drivers/utils.d b/source/ddbc/drivers/utils.d index 9e71150..742f1fc 100644 --- a/source/ddbc/drivers/utils.d +++ b/source/ddbc/drivers/utils.d @@ -58,10 +58,7 @@ SysTime parseSysTime(const string timestampString) @safe { return SysTime(parseDateTime(timestampString), UTC()); } } catch (ConvException e) { - // static if(__traits(compiles, (){ import std.experimental.logger; } )) { - // import std.experimental.logger : sharedLog; - // sharedLog.error("Could not parse " ~ timestampString ~ " to SysTime", e); - // } + // error("Could not parse " ~ timestampString ~ " to SysTime", e); throw new DateTimeException("Can not convert '" ~ timestampString ~ "' to SysTime"); } } @@ -103,10 +100,7 @@ DateTime parseDateTime(const string timestampString) @safe { } throw new DateTimeException("Can not convert " ~ timestampString); } catch (ConvException e) { - // static if(__traits(compiles, (){ import std.experimental.logger; } )) { - // import std.experimental.logger : sharedLog; - // sharedLog.error("Could not parse " ~ timestampString ~ " to SysTime", e); - // } + // error("Could not parse " ~ timestampString ~ " to SysTime", e); throw new DateTimeException("Can not convert '" ~ timestampString ~ "' to DateTime"); } } diff --git a/source/ddbc/pods.d b/source/ddbc/pods.d index 7b2e2e8..b46fb57 100644 --- a/source/ddbc/pods.d +++ b/source/ddbc/pods.d @@ -55,6 +55,11 @@ import std.conv; import std.datetime; import std.string; import std.variant; +static if (__traits(compiles, () { import std.logger; } )) { + import std.logger; +} else { + import std.experimental.logger; +} static import std.ascii; @@ -1075,29 +1080,14 @@ bool insert(T)(Statement stmt, ref T o) if (__traits(isPOD, T)) { // https://issues.dlang.org/show_bug.cgi?id=18780 // https://github.com/dlang/phobos/pull/7954 if(insertId.convertsTo!(typeof(o.id))) { - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - import std.experimental.logger ; sharedLog; - sharedLog.tracef("The ID is of type '%s' and can be a '%s'", insertId.type().toString(), typeof(o.id).stringof); - } + tracef("The ID is of type '%s' and can be a '%s'", insertId.type().toString(), typeof(o.id).stringof); o.id = insertId.get!(typeof(o.id)); // potentially could use coerce instead of get } else if(is(typeof(o.id) == uint) || is(typeof(o.id) == int)) { // This isn't generally an issue but on Windows (x86) using a size_t will result in a uint - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - import std.experimental.logger ; sharedLog; - sharedLog.warningf("The ID is of type '%s', converting to type '%s' could cause data errors", insertId.type().toString(), typeof(o.id).stringof); - } else { - import std.stdio : writeln; - writeln("The ID is of type '" ~ insertId.type().toString() ~ "', converting to type '" ~ typeof(o.id).stringof ~ "' could cause data errors"); - } + warningf("The ID is of type '%s', converting to type '%s' could cause data errors", insertId.type().toString(), typeof(o.id).stringof); o.id = to!(typeof(o.id))(insertId.get!ulong); // alternative syntax: o.id = cast(typeof(o.id))insertId.get!ulong; } else { - static if(__traits(compiles, (){ import std.experimental.logger; } )) { - import std.experimental.logger ; sharedLog; - sharedLog.errorf("The ID is of type '%s' and cannot be converted to type '%s'", insertId.type().toString(), typeof(o.id).stringof); - } else { - import std.stdio : writeln; - writeln("The ID is of type '" ~ insertId.type().toString() ~ "' and cannot be converted to type: " ~ typeof(o.id).stringof); - } + errorf("The ID is of type '%s' and cannot be converted to type '%s'", insertId.type().toString(), typeof(o.id).stringof); } return true; diff --git a/test/ddbctest/common.d b/test/ddbctest/common.d index 246e37a..a7254c5 100644 --- a/test/ddbctest/common.d +++ b/test/ddbctest/common.d @@ -21,15 +21,19 @@ class DdbcTestFixture { this.setupSql = setupSql; this.teardownSql = teardownSql; - static if(__traits(compiles, (){ import std.experimental.logger; } )) { + static if (__traits(compiles, (){ import std.logger; } )) { + import std.logger : globalLogLevel, sharedLog, LogLevel; + import std.logger.core : StdForwardLogger; + } else { import std.experimental.logger : globalLogLevel, sharedLog, LogLevel; import std.experimental.logger.core : StdForwardLogger; - //pragma(msg, "Setting 'std.experimental.logger : sharedLog' to use 'stdout' logging..."); - globalLogLevel(LogLevel.all); - //import std.experimental.logger.filelogger : FileLogger; - //sharedLog = new FileLogger(stdout); - //sharedLog = new StdForwardLogger(LogLevel.all); } + + //pragma(msg, "Setting 'std.logger : sharedLog' to use 'stdout' logging..."); + globalLogLevel(LogLevel.all); + //import std.logger.filelogger : FileLogger; + //sharedLog = new FileLogger(stdout); + //sharedLog = new StdForwardLogger(LogLevel.all); } @BeforeEach @@ -62,4 +66,4 @@ class DdbcTestFixture { //debug writeln("@AfterEach : closing db connection"); conn.close(); } -} \ No newline at end of file +} diff --git a/test/ddbctest/main.d b/test/ddbctest/main.d index 224a36f..2db54f4 100644 --- a/test/ddbctest/main.d +++ b/test/ddbctest/main.d @@ -13,6 +13,8 @@ import ddbc.test.common : DdbcTestFixture; import ddbc.core : Connection, PreparedStatement, Statement, SQLException; import ddbc.pods; +static import ddbc.core; + version(USE_MYSQL) { pragma(msg, "DDBC test will run MySQL tests");