Java compiler cannot find "write"

User avatar
seal58
Posts: 377
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

Re: Java compiler cannot find "write"

Post by seal58 »

Hi Reid,

I am very interestet in such a tool, at least in order to see how to manage text files with Java.

Roland
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: Java compiler cannot find "write"

Post by UrbanCyborg »

Okay, here it is:

Code: Select all

// LogUtility.java                              //

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                        //
//  This file and all related files Copyright (C) 2022 by Reid Sweatman.  All rights reserved.                                            //
//                                                                                                                                        //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                        //
//           Program    Voltage Modular                                                                                                   //
//                                                                                                                                        //
//            Module    LogUtility                                                                                                        //
//                                                                                                                                        //
//       Description    Implements a simple logging class                                                                                 //
//                                                                                                                                        //
//        Programmer    Reid Sweatman                                                                                                     //
//                                                                                                                                        //
//  Revision History    v1.00    08/28/2022   Initial version                                                                             //
//                                                                                                                                        //
//             Notes                                                                                                                      //
//                                                                                                                                        //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

package com.cyberwerkshi.utility;

// Imports ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

import java.io.IOException;
import java.util.logging.Formatter;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.function.Supplier;
import java.util.logging.SimpleFormatter;
import java.lang.String;


////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//                                                                                                                                        //
//        Class Name    LogUtility                                                                                                        //
//                                                                                                                                        //
//       Description    Class that implements a simple logging facility                                                                   //
//                                                                                                                                        //
//  Globals Modified                                                                                                                      //
//                                                                                                                                        //
//  Public Interface    LogUtility()                   Default constructor                                                                //
//                      LogUtility()                   Constructor                                                                        //
//                      LogUtility()                   Copy constructor                                                                   //
//                     ~LogUtility()                   Destructor                                                                         //
//                      operator=()                    Overloaded assignment operator                                                     //
//                                                                                                                                        //
//             Notes                                                                                                                      //
//                                                                                                                                        //
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

public class LogUtility {

    private Handler       fileHandler;
    private static Logger LOGGER;
    private Formatter     simpleFormatter;

    public LogUtility(
        String loggedPkg,                                               // name of the logged package (dot hierarchy)
        String logLocation                                              // pathname to the log file in the filesystem
    ) {
        try {
            LOGGER          = Logger.getLogger(loggedPkg);
            fileHandler     = new FileHandler(logLocation);
            simpleFormatter = new SimpleFormatter();
            LOGGER.addHandler(fileHandler);
            fileHandler.setFormatter(simpleFormatter);
            fileHandler.setLevel(Level.ALL);
            LOGGER.setLevel(Level.ALL);
            LOGGER.info("Logger Name: " + LOGGER.getName());
        }
        catch(IOException exception){
            LOGGER.log(Level.SEVERE, "Log FileHandler Error", exception);
        }
    }   // LogUtility()

    public void Log(
        Level  level,                                                   // importance level (enum)
        String msg                                                      // message to be logged
    ) {
        LOGGER.log(level, msg);
    }   // Log(Level, String)

//    public void Log(
//        LogRecord record                                                // LogRecord to be logged
//    ) {
//        LOGGER.log(LogRecord record);
//    }   // Log(LogRecord)

    public void Log(
        Level            level,                                         // importance level (enum)
        Supplier<String> msgSupplier                                    // function to supply the message to be logged
    ) {
        LOGGER.log(level, msgSupplier);
    }   // Log(LogRecord, Supplier<String>)

    public void Log(
        Level  level,                                                   // importance level (enum)
        String msg,                                                     // message to be logged
        Object param1                                                   // parameter to the message
    ) {
        LOGGER.log(level, msg, param1);
    }   // Log(Level, String, Object)

    public void Log(
        Level     level,                                                // importance level (enum)
        String    msg,                                                  // message to be logged
        Object [] params                                                // array of parameters to the message
    ) {
        LOGGER.log(level, msg, params);
    }   // Log(Level, String, Object [])

    public void Log(
        Level     level,                                                // importance level (enum)
        String    msg,                                                  // message to be logged
        Throwable thrown                                                // throwable associated with log message
    ) {
        LOGGER.log(level, msg, thrown);
    }   // Log(Level, String, Throwable)

    public void Log(
        Level            level,                                         // importance level (enum)
        Throwable        thrown,                                        // throwable associated with log message
        Supplier<String> msgSupplier                                    // function to supply the message to be logged
    ) {
        LOGGER.log(level, thrown, msgSupplier);
    }   // Log(Level, Throwable, Supplier<String>)

};  // class LogUtility()


// End of File ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
I suppose it wasn't really a bright idea to handle the caught exception with an instance of the code that caused the exception, but what the hey, it's never triggered the exception for me. :D Sorry for the 140-char margins and maybe the 4-char indents, but it's what I work with.

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
Aarnville
Posts: 53
Joined: Sat Jun 18, 2022 5:14 pm

Re: Java compiler cannot find "write"

Post by Aarnville »

poetix wrote: Thu Feb 09, 2023 3:00 pm It struck me in an evil moment that one could actually stream characters to a text console (implemented as a debug display module) over an audio connection...
I do exactly that. The great thing is that because the signal is "analogue" and the sample rates are fixed it's very simple to do - you are not messing with bit streams or clocks. The transmitting end encodes each character as a voltage and puts it on the tx socket for one sample time or puts it to zero volts if there is nothing to send. At the receiving end any non-zero voltage in a sample "tick" is a valid character.

The important thing is to avoid generating the strings in ProcessSample. Just set some vars and flags and generate the string a bit later on a timer event. You can even capture the system time in ProcessSample and include it in the message. Of course, you have to keep in your mind that the debug stuff is adding to the system load and make sure you remove or disable it for production code.
Centripidity
Posts: 146
Joined: Sun Jan 22, 2023 5:18 am
Location: Melbourne
Contact:

Re: Java compiler cannot find "write"

Post by Centripidity »

This is off-topic but probably still worth mentioning. To help debugging in ProcessSample() I create a global variable,

Code: Select all

long tick = 0;
At the start of ProcessSample() I put

Code: Select all

tick++;
and then at the appropriate location something like,

Code: Select all

   if(tick%48000 == 0)
   {
      Log("Msg: ");
   }
It's not a general solution but it can be useful at times.
User avatar
seal58
Posts: 377
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

Re: Java compiler cannot find "write"

Post by seal58 »

UrbanCyborg wrote:
Okay, here it is:
Hi Reid,
that's great. Thank you for publishing your code. I just went trugh it and found a hierarchy of methods. Hopefully I will understand what all the methods are doing.
Aarville wrote:
poetix wrote: ↑Thu Feb 09, 2023 4:00 pm
It struck me in an evil moment that one could actually stream characters to a text console (implemented as a debug display module) over an audio connection...
I do exactly that. The great thing is that because the signal is "analogue" and the sample rates are fixed it's very simple to do - you are not messing with bit streams or clocks. The transmitting end encodes each character as a voltage and puts it on the tx socket for one sample time or puts it to zero volts if there is nothing to send. At the receiving end any non-zero voltage in a sample "tick" is a valid character.
Coding characters by CV is a very interesting idea. It reminds me on LSSP from Adroid Synthesis where time is coded to CV. So a display module with CV text interface seems to be a solvable next job for me. :)

Roland
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: Java compiler cannot find "write"

Post by UrbanCyborg »

In re the code I posted, I'm embarrassed to note that the class comment box has auto-filled comments for C++. Missed those when I copied the editor template from one for that language.

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
seal58
Posts: 377
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

Re: Java compiler cannot find "write"

Post by seal58 »

Hi Reid,

I just made a little test module, where I used your LogUtility class. It works fine.

Based on an example in Java Docs:

Code: Select all

Some example formats:

    java.util.logging.SimpleFormatter.format="%4$s: %5$s [%1$tc]%n"

    This prints 1 line with the log level (4$), the log message (5$) and the timestamp (1$) in a square bracket.

         WARNING: warning message [Tue Mar 22 13:11:31 PDT 2011]
         
I added an interface method that should make it possible to change date and time format in log records:

Code: Select all

    public void format(
        String fstr                   // format string;
    ){
        simpleFormatter.format = fstr;  
    }
Java compiler tells me "cannot find symbol 'format' ", though he should. "format" is a property of SimpleFormatter. I tryed adding an import statement of various variants whithout getting it compiled.

Do you have any idea what I did wrong?

Roland
UrbanCyborg
Posts: 625
Joined: Mon Nov 15, 2021 9:23 pm

Re: Java compiler cannot find "write"

Post by UrbanCyborg »

Roland, I think the problem you're having is detailed in this excerpt from the docs:
The SimpleFormatter is initialized with the format string specified in the java.util.logging.SimpleFormatter.format property to format the log messages. This property can be defined in the logging properties configuration file or as a system property. If this property is set in both the logging properties and system properties, the format string specified in the system property will be used. If this property is not defined or the given format string is illegal, the default format is implementation-specific.
I believe that your code doesn't work because you're not defining the property in either of the two allowed ways. It's not clear from your posted example what scope your format() function is defined in; it's possible that the copy of simpleFormatter isn't in the scope of your function. Whatever, I don't think it's at system scope.

Another problem might be that in the first usage you quote, you prefix the property with the full package path, and in the second, you don't. Since I can't look at your imports, I don't know if they're both allowed usages.

For any usage except the initial logging message you can simply format the output string to suit yourself. What follows is a somewhat extreme example of what you can do that I used in debugging Poly Switch:

Code: Select all

    private enum lB { NONE, ONE, TWO };

    // Log toggle array
    private void LogToggles(String heading, lB lineBreaks) {
        String logString = heading +
                           "\nToggles:      " +
                           (aToggles[0].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[1].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[2].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[3].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[4].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[5].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[6].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[7].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[8].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[9].GetValue()  == 1.0 ? "1 " : "0 ") +
                           (aToggles[10].GetValue() == 1.0 ? "1 " : "0 ") +
                           (aToggles[11].GetValue() == 1.0 ? "1 " : "0 ") +
                           (aToggles[12].GetValue() == 1.0 ? "1 " : "0 ") +
                           (aToggles[13].GetValue() == 1.0 ? "1 " : "0 ") +
                           (aToggles[14].GetValue() == 1.0 ? "1 " : "0 ") +
                           (aToggles[15].GetValue() == 1.0 ? "1 " : "0 ") +
                           " isInitializing: " + isInitializing                                       +
                           "  All-None: "      + (allNoneToggle.GetValue() == 1.0 ? "All " : "None ") +
                           "  Solo: "          + (soloToggle.GetValue() == 1.0 ? "On " : "Off ")      +
                           "  lastSolo: "      + lastSolo + "\n" +
                           "Toggle Saves: "                      +
                           (toggleSave[0]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[1]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[2]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[3]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[4]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[5]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[6]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[7]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[8]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[9]  == 1.0 ? "1 " : "0 ") +
                           (toggleSave[10] == 1.0 ? "1 " : "0 ") +
                           (toggleSave[11] == 1.0 ? "1 " : "0 ") +
                           (toggleSave[12] == 1.0 ? "1 " : "0 ") +
                           (toggleSave[13] == 1.0 ? "1 " : "0 ") +
                           (toggleSave[14] == 1.0 ? "1 " : "0 ") +
                           (toggleSave[15] == 1.0 ? "1 " : "0 ") +
                           (lineBreaks == lB.NONE ? "" : (lineBreaks == lB.ONE ? "\n" : "\n\n"));
        rLog.Log(Level.FINE, logString);
    }   // LogToggles()
I hope any of this helps.

Reid
Cyberwerks Heavy Industries -- viewforum.php?f=76
User avatar
seal58
Posts: 377
Joined: Fri Jul 12, 2019 5:28 pm
Location: Rostock, Germany
Contact:

Re: Java compiler cannot find "write"

Post by seal58 »

Hi Reid,

sorry for bombing you with my newbee problem.
Basically I know how to build my own log messages. But before each message there is another line with date, time and name of your package and I could not find out where it is defined.
For my debugging purposes I don't need date and time.

At the moment I get this:

Feb. 13, 2023 6:41:11 PM com.cyberwerkshi.utility.LogUtility <init>
INFO: Logger Name: com.polymond.safedatatest3
Feb. 13, 2023 6:41:11 PM com.cyberwerkshi.utility.LogUtility Log
CONFIG: GetStateInformation()
Feb. 13, 2023 6:41:11 PM com.cyberwerkshi.utility.LogUtility Log
FINE: creating bufHeaderArray[13]
Feb. 13, 2023 6:41:14 PM com.cyberwerkshi.utility.LogUtility <init>
INFO: Logger Name: com.polymond.safedatatest3
Feb. 13, 2023 6:41:14 PM com.cyberwerkshi.utility.LogUtility Log
CONFIG: GetStateInformation()
Feb. 13, 2023 6:41:14 PM com.cyberwerkshi.utility.LogUtility Log
FINE: creating bufHeaderArray[13]
Feb. 13, 2023 6:41:19 PM com.cyberwerkshi.utility.LogUtility Log
CONFIG: GetStateInformation()
Feb. 13, 2023 6:41:19 PM com.cyberwerkshi.utility.LogUtility Log
FINE: creating bufHeaderArray[13]


My own messages are hardly to be seen between these long lines.
I think that normally my local system should use local time date format, but it does not. So my plan was to config "simpleFormatter", what is an instance of SimpleFormatter within your class LogUtility. The little method I added to your class, should transfer a format string from outer world to simpleFormatter inside of your class.

For import I tryed (one of these only at a time):

Code: Select all

import java.util.logging.SimpleFormatter.*;
import java.util.logging.SimpleFormatter.format;
import java.util.logging.SimpleFormatter.format.*;
import java.util.logging.Formatter;
import java.util.logging.Formatter.*;
import java.util.logging.Formatter.format;
I'm afraid that syntax of my statement simpleFormatter.format = fstr; is not ok, though it matches Java doc example.

Roland
terrymcg
Posts: 85
Joined: Mon Apr 08, 2019 12:35 am

Re: Java compiler cannot find "write"

Post by terrymcg »

Hi Roland...
seal58 wrote: Tue Feb 14, 2023 12:13 pm My own messages are hardly to be seen between these long lines.
This is the same problem faced when trying to read the logs generated by VM/VMD, and if you just want to make your output stand out better (vs digging deeply into Java logging) you could prefix all of your messages with some unique string (something like ">>>>>>" say) then use a text editor/script/whatever to filter for only those lines in your log output.

Not that getting into Java logging isn't a worthwhile undertaking, but I know I dislike getting bogged down in coding stuff that's non-primary to whatever module I'm working on. Your mileage may vary ;)

Cheers,
--
Terry McG
Post Reply

Return to “Module Designer”