Recent

Author Topic: [Sorted] WriteLn sometimes does not write to terminal window on MacOs  (Read 625 times)

Wilko500

  • Jr. Member
  • **
  • Posts: 73
Lazarus = 3.99 FPC = 3.3.1. MacOs Monterey 12.7.5

I am attempting to debug what was a working GUI program, a change in input data seems to have caused occasional records to be "lost".  Since I am developing on MACOS I cannot access the message window as I think can be done in Windoze environment.  However I can (usually) manage by adding WriteLn statements at appropriate places in the code and then running my compiled program from a terminal window. ShowMessage does output but this is not a convenient solution as I have to be present to look and copy output if necessary.  I would like to be able to leave the conversion running and then come back later to copy/look at all the debug output.

My program is started from a button in Unit1, it calls a function in another Unit to add records to my database.   WriteLn statements in the main Unit1 appear in terminal window as expected.  WriteLn statements in the second unit do not output to the terminal window. It seems that it only from the second Unit that I cannot output to terminal window.

I would appreciate help or suggestions to fix this problem.

The terminal window output and function code is as follows.  I have indicated where output is called and where I would have expected it to occur in the output
Code: Pascal  [Select][+][-]
  1. Table Units Created
  2. Table Inverters Created
  3. Table TmpDays Created
  4. Table Days Created
  5. Table Times Created
  6. 1 records added to table Units     <<<<<WriteLn output should appear here
  7. 3 records added to table Inverters
  8. 2947 records added to table TmpDays
  9. Duplicate Time Record found: 2018/02/18 14:06
  10. Duplicate Time Record found: 2019/03/16 13:52
  11. Duplicate Time Record found: 2020/07/25 11:09
  12. Duplicate Time Record found: 2021/10/17 11:17
  13. In TblDataAddDaysTimes: Date 2024/02/24 No matching time records
  14. In TblDataAddDaysTimes: Date 2024/02/25 No matching time records
  15. In TblDataAddDaysTimes: Date 2024/02/26 No matching time records
  16. In TblDataAddDaysTimes: Date 2024/02/27 No matching time records
  17. 2943 records added to table Days
  18. 218308 records added to table Times
 

The Function code is
Code: Pascal  [Select][+][-]
  1. Function tblDataAddInverters(Const txtFile:String; Out icount:int32) : boolean;
  2. //Read comma delimeted file and write inverter records to database table
  3. Var
  4.   iRecs: Int32;  //record count of added records
  5.   txtF:  TextFile;
  6.   strLine: String;
  7.   strArray: TStringArray;
  8.   strColNames: String;
  9.   strColUnits: String;
  10. begin
  11. Result:=False;
  12. iRecs:=0;
  13. Try
  14.   //Lookup ColNames & ColUnits form table Units
  15.   strColNames:=GetStringFromTable('ColNames', 'Units', 'UnitID', '1');
  16.   strColUnits:=GetStringFromTable('ColUnits', 'Units', 'UnitID', '1');
  17.   AssignFile(txtF, txtFile);
  18.   Reset(txtF);
  19.   dbTrans.Active:=True;
  20.   ReadLn(txtF, strLine);  //Skip over column headers
  21.   While not EOF(txtF) do
  22.     Begin
  23.     ReadLn(txtF, strLine);
  24.     iRecs:= iRecs + 1;
  25.     WriteLn(strLine);     //// <<<<<<<<<<<<<<<<<<<<<no output
  26.     ShowMessage(IntToStr(iRecs) + ' ' +strLine);
  27.  
  28.     strArray:=strLine.Split(',');
  29.     //Insert Records here
  30.     sqlite3.ExecuteDirect('INSERT INTO "Inverters" (' +
  31.                           '"SystemName", ' +
  32.                           '"ModelNo", ' +
  33.                           '"MPPT", ' +
  34.                           '"ColNames", ' +
  35.                           '"ColUnits"' +
  36.                           ' )' +
  37.                           'VALUES (' +
  38.                           //DoNullStr(strArray[0]) + ',' +
  39.                           DoNullStr(strArray[1]) + ',' +
  40.                           DoNullStr(strArray[2]) + ',' +
  41.                           DoNullStr(strArray[3]) + ',' +
  42.                           QuotedStr(strColNames) + ',' +
  43.                           QuotedStr(strColUnits) +
  44.                           ')');
  45.  
  46.                           //'"' + strColNames + '",' +
  47.                           //'"' + strColUnits + '"' +
  48.  
  49.   Form1.txtInverters.Text:=IntToStr(iRecs);   //Take care here to avoid circular reference
  50.   {EndWhile}End;
  51.   //Commit changes to DataBase
  52.   dbTrans.Commit;
  53.   iCount:=iRecs;
  54.   Result:=True;
  55.   //Close text file
  56.   Close(txtF);
  57. Except
  58.   On E: Exception  Do
  59.     Begin
  60.     WriteLn('Error in tblDataAddInverters :' + E.Message);
  61.     Result:=False;
  62.     iCount:=0;
  63.   End;
  64. {EndTry}end;
  65. end;
« Last Edit: May 22, 2024, 12:16:29 am by Wilko500 »
MacBook Pro mid 2015 with OS Monterey 12.7.2
FPC 3.3.1 Lazarus _3_0

jamie

  • Hero Member
  • *****
  • Posts: 6301
Re: WriteLn sometimes does not write to terminal window on MacOs
« Reply #1 on: May 22, 2024, 12:00:35 am »
I can only offer you a suggestion.

 You are using Pascal IO stuff which has to pass the sniff test with the IOResult variable.

 So, if you have any pending errors without using IoResult =? or simply InOutRes := 0; the writeln make simply not produce anything.

The only true wisdom is knowing you know nothing

Wilko500

  • Jr. Member
  • **
  • Posts: 73
Re: WriteLn sometimes does not write to terminal window on MacOs
« Reply #2 on: May 22, 2024, 12:15:40 am »
Thanks Jamie.  I was experimenting with IOResult last week but that's another question. And indeed I suspected some sort of error with the SQL.   

However, just a few minutes ago it started working as it should. The solution was I think, and I stress I think, that running from terminal window was picking up a different version of the copied program to the IDE. The path changed so I think I must have made a mistake!!

I am very sorry to have submitted an unnecessary post. If I can I will delete

 



MacBook Pro mid 2015 with OS Monterey 12.7.2
FPC 3.3.1 Lazarus _3_0

 

TinyPortal © 2005-2018