Create a log file

Hello all,

I created a dll with NX Journal code, and i want to create a log to follow the use of this dll.
So I put this method in my code

void write_log(const char *msg,const char *filename_log = "C:\\temp.log"){

std::ofstream ofs;

ofs.open(filename_log,std::ofstream::app);

NXOpen::UI::GetUI()->NXMessageBox()->Show("Attention", NXMessageBox::DialogTypeInformation,Exception);

if(ofs.is_open()){
time_t a;
time(&a);
ofs << ctime(&a) << ' ' << "Execution " << msg <<" par l'utilisateur : "<< std::getenv("USERNAME") << std::endl;

ofs.close();
ofs.flush();
ofs.clear();
}
else
{
NXOpen::UI::GetUI()->NXMessageBox()->Show("Attention", NXMessageBox::DialogTypeInformation,"Error");
}

}

And i called it once. SO the first time it works great. But if I try to execute again my NXOPEN dll, it doesn't works, it seems to be that the ofs isn't clear and NX still have a process with the textfile...

Have you got an idea ?

Thanks for your help,

Romain

I'm not sure this will solve your problem, but you need to specify an "unload option". In Visual Basic, this takes the form of a function in the module and it looks like this:





Public Function GetUnloadOption(ByVal dummy As String) As Integer

'Unloads the image when the NX session terminates
GetUnloadOption = NXOpen.Session.LibraryUnloadOption.AtTermination

'----Other unload options-------
'Unloads the image immediately after execution within NX
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Immediately

'Unloads the image explicitly, via an unload dialog
'GetUnloadOption = NXOpen.Session.LibraryUnloadOption.Explicitly
'-------------------------------

End Function



AtTermination option means NX will keep the code loaded in memory until NX terminates.


Immediately option means NX will unload the code as soon as it finishes executing.


Explicitly option means that you must specify when it gets unloaded.




I'd guess that your code is getting unloaded when it shouldn't or vice versa. Consult the programmer's guide in the NX help docs to get more direction on which option suits your needs.

Hello,

Thank you for your answer but still have the same problem...

You should check the log file to determine what error is occurring the second execution. I had the same problem initially when I began compiling programs but it was because I wasn't unloading.

DHuskic
Nx 9 VB

Hello DHuskic,

In my log file, nothing is catched for this problem.
I tried lot of things in my code but still the same problem. When the program opens for the first time the file with ofs.open(filename_log,std::ofstream::app);
, it works, and ofs.close() is executed. I think it is a NX problem, maybe the NX process lock the file ? Because on the second execution, if i do the same ofs.open(filename_log,std::ofstream::app);
, i don't have error, but the ofs is still empty. So I think the problem is NX process

Thanks for your help...

I think DHuskic is referring to the NX log file, not the log file you are trying to create. The NX log file can be opened by going to the NX menu: Help -> Log file. The log file keeps track of all the actions in NX, including what happens when your code is run. The log file will be quite large, after running (or attempting to run) your code, scroll to near the bottom of the log file and look for any error messages.

Yes, I understood.
And as you can see, there is no information in this NXlog ...

1st time i execute : OK :
&MACRO FOCUS CHANGE IN 1
&MACRO MESSAGE_BOX 1, Box must be active. Is it ok ?

Teamcenter Integration Load Options Are :-
Load Options : With Revision Rules : Latest Working

2nd time i execute : didn't write in my text file
&MACRO FOCUS CHANGE IN 1
&MACRO MESSAGE_BOX 1, Box must be active. Is it ok ?

Teamcenter Integration Load Options Are :-
Load Options : With Revision Rules : Latest Working

I write in my text file after the Messagebox...

Somewhere in the NX log file it should show where your dll gets loaded/called, check this section for error messages after each run of your code.

Also, you can write your own messages to the log file, perhaps in your code you could add some debugging messages such as:
does C:\temp.log exist? true/false
opened C:\temp.log successfully: true/false
etc.

Thanks for your help. I put test in my code to check if the file is closed, and even after the ofs.close(); , the file is still open. I think it is NX which lock the file. I tried the ufusr_ask_unload(); before and after the ofs.close(); but same problem...

In the NX Log file, all modules are loaded and i have no errors

The way I have implimented logging is to create a new file each time the script is used. The filename includes the date and time (to seconds) and the username to ensure that it is unique. This overcomes the issue of 2 users trying to access the log file at the same time. (We have around 200 CAD users). I use an Excel macro to compile all the information from the individual logfiles.

Mike

Thank you for your help. I think i have no other choice, and i will implement your solution... it's a shame that the file never close until nx process is closed...