object.Exception at std/stdio.d(1321): Enforcement failed - I must be doing something wrong?

Colin Grogan grogan.colin at gmail.com
Thu Aug 15 10:40:59 PDT 2013


Hi Ali,

Heres my full Logger class.

module utils.log;

import std.stdio;
import std.string;
import std.datetime;
public enum LogLevel {Fatal=0, Severe=1, Info=2, Debug=3, 
Verbose=4};

public class Logger{
     public:

     this(LogLevel minLevel = LogLevel.Info, string 
fileName="logfile.log"){
         this.minLevel = minLevel;
         logFile = File(fileName, "w");
         this.writeMsg(format("Opened file for writing at [%s]", 
currTimestamp()));
         logFile.flush();
         scope(exit){
             logInfo("End log");
             logFile.close();
         }
     }
     void logFatal(string message){
         this.log(LogLevel.Fatal, "FATAL - " ~ message);
     }
     void logSevere(string message){
         this.log(LogLevel.Severe, "SEVERE - " ~ message);
     }
     void logInfo(string message){
         this.log(LogLevel.Info, format("INFO - %s",message));
     }
     void logDebug(string message){
         this.log(LogLevel.Debug, "DEBUG - " ~ message);
     }
     void logVerbose(string message){
         this.log(LogLevel.Verbose, "VERBOSE - " ~ message);
     }

     private:
     void writeMsg(string msg){
         logFile.writeln(msg);
     }
     void log(LogLevel level, string message){
         if(this.minLevel <= level){
             writefln("Before write! %s",message);
             string timestamp = currTimestamp();
             this.writeMsg(format("[%s] %s", timestamp, message));
             writefln("After write! %s", message);
             logFile.flush();
         }
     }
     string currTimestamp(){
         auto currTime = Clock.currTime();
         return currTime.toISOExtString()[0..$-8];
     }

     private:
         LogLevel minLevel;
         File logFile;
}

logFile is a class variable, so its definatly visible.
Ill go with adding to your code now and see at what point it 
breaks.

Thanks!

On Thursday, 15 August 2013 at 17:33:11 UTC, Ali Çehreli wrote:
> On 08/15/2013 10:03 AM, Colin Grogan wrote:
>
> > I've done this a million times (I thought!) but I'm getting a
> strange
> > error I cant figure out.
> >
> > The code:
> >
> > void writeMsg(string msg){
> >        logFile.writeln(msg);
>
> What is logFile?
>
> > }
> >
> > is failing with this error:
> >
> > object.Exception at std/stdio.d(1321): Enforcement failed
> > ----------------
> > ./tester(pure @safe bool
> std.exception.enforce!(bool).enforce(bool, lazy
> > const(char)[], immutable(char)[], ulong)+0x6b) [0x49a343]
> > ./tester(ref std.stdio.File.LockingTextWriter
> > std.stdio.File.LockingTextWriter.__ctor(ref
> std.stdio.File)+0x59)
> > [0x4ce019]
>
> ...
>
> > Also, I'm creating the logFile variable as follows:
> > File logFile = File(fileName, "w");
>
> That looks like a local variable. How does writeMsg see it?
>
> > If ye need the code around it I'll be glad to post it
>
> It is very helpful if you can reproduce it in a minimal 
> example. The following program works with v2.064-devel-52cc287:
>
> import std.stdio;
>
> enum fileName = "deneme.txt";
> File logFile;
>
> static this() {
>     logFile = File(fileName, "w");
> }
>
> void writeMsg(string msg){
>       logFile.writeln(msg);
> }
>
> void main()
> {
>     writeMsg("hello");
>     writeMsg("world");
> }
>
> Can you add to it and see when the problem appears.
>
> Ali


More information about the Digitalmars-d-learn mailing list