Skip to content

Commit

Permalink
Fixes #112: Migrate to std.logger (no longer experimental since DMD 2…
Browse files Browse the repository at this point in the history
….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.
  • Loading branch information
vnayar authored Jan 7, 2023
1 parent b29deb3 commit bd82f0c
Show file tree
Hide file tree
Showing 10 changed files with 72 additions and 105 deletions.
7 changes: 5 additions & 2 deletions example/source/testddbc.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
20 changes: 8 additions & 12 deletions source/ddbc/common.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -162,24 +164,20 @@ 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);
//freeConnections.length = oldSize - 1; // some bug in remove? length is not decreased...
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");
Expand All @@ -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;
}
}
Expand Down
18 changes: 7 additions & 11 deletions source/ddbc/drivers/mysqlddbc.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
38 changes: 14 additions & 24 deletions source/ddbc/drivers/odbcddbc.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -622,9 +624,7 @@ version (USE_ODBC)
scope (exit)
unlock();

static if(__traits(compiles, (){ import std.experimental.logger; } )) {
sharedLog.trace(query);
}
trace(query);

try
{
Expand Down Expand Up @@ -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
{
Expand All @@ -682,9 +680,7 @@ version (USE_ODBC)
scope (exit)
unlock();

static if(__traits(compiles, (){ import std.experimental.logger; } )) {
sharedLog.trace(query);
}
trace(query);

try
{
Expand Down Expand Up @@ -1011,9 +1007,7 @@ version (USE_ODBC)
scope (exit)
unlock();

static if(__traits(compiles, (){ import std.experimental.logger; } )) {
sharedLog.trace(stmt);
}
trace(stmt);

try
{
Expand All @@ -1037,9 +1031,7 @@ version (USE_ODBC)
scope (exit)
unlock();

static if(__traits(compiles, (){ import std.experimental.logger; } )) {
sharedLog.trace(stmt);
}
trace(stmt);

try
{
Expand Down Expand Up @@ -1069,9 +1061,7 @@ version (USE_ODBC)
scope (exit)
unlock();

static if(__traits(compiles, (){ import std.experimental.logger; } )) {
sharedLog.trace(stmt);
}
trace(stmt);

try
{
Expand Down Expand Up @@ -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);
Expand Down
18 changes: 7 additions & 11 deletions source/ddbc/drivers/pgsqlddbc.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
20 changes: 8 additions & 12 deletions source/ddbc/drivers/sqliteddbc.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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]);
Expand Down Expand Up @@ -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;
}
Expand All @@ -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);
}
Expand Down
10 changes: 2 additions & 8 deletions source/ddbc/drivers/utils.d
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}
Expand Down Expand Up @@ -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");
}
}
Expand Down
26 changes: 8 additions & 18 deletions source/ddbc/pods.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
Expand Down
Loading

0 comments on commit bd82f0c

Please sign in to comment.