Skip to content

Commit

Permalink
Add Runtime.logRuntimeException() API
Browse files Browse the repository at this point in the history
  • Loading branch information
Gene Gleyzer committed May 9, 2024
1 parent 3d0bc68 commit 7bc3668
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 21 deletions.
12 changes: 12 additions & 0 deletions javatools/src/main/java/org/xvm/runtime/Runtime.java
Expand Up @@ -132,6 +132,18 @@ public void setDebuggerActive(boolean fActive)
m_fDebugger = fActive;
}

/**
* Log a runtime exception. Eventually, this will go to a log file...
*
* @return an error tag the logged exception
*/
public static String logRuntimeException(String sErr)
{

This comment has been minimized.

Copy link
@lagergren

lagergren May 9, 2024

Contributor

I think it would be better if we started on a "standardised" way to do logging. If we could use a simple slf4j API for all our logging needs instead of calling System.err, it would be simple to extend, add multiple log sinks, turn into structured logging, or add something that logs data that we save and mine in big query from all our customers, or whatever we want to do.

The Gradle logger is already an extension of the slf4j api, and there is a very simple implementation of it on mavenCentral and everywhere else that just prints to stdout or stderr, but still uses a logger with different log levels, compatible with the superclass of the Gradle logger.

https://www.slf4j.org/manual.html

Using the api (compile classpath level) and the slf4j-simple artifact as the implementation will give you a bare bones console printing abstracted logger that can be replaced with better implementations the more functionality we need, keeping the log calls unchanged, regardless of implementation.

https://mvnrepository.com/artifact/org.slf4j/slf4j-api
https://mvnrepository.com/artifact/org.slf4j/slf4j-simple

Ready made artifacts above. Luckily we speak Maven. Just slam them into libs.versions.toml, add some dependencies to the runtime classes and replace-in-files your System.err.println to logger.info or whatever it is ...

This comment has been minimized.

Copy link
@lagergren

lagergren May 9, 2024

Contributor

I mean, we can do whatever we want for logging, but it would be great if we used the same interfaces throughout the code base.

String sTag = "RTError: " + System.currentTimeMillis();
System.err.println("*** " + sTag + '\n' + sErr);
return sTag;
}


// ----- constants and fields ------------------------------------------------------------------

Expand Down
Expand Up @@ -28,6 +28,7 @@
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.ObjectHandle.DeferredCallHandle;
import org.xvm.runtime.Runtime;
import org.xvm.runtime.ServiceContext;
import org.xvm.runtime.TypeComposition;
import org.xvm.runtime.Utils;
Expand Down Expand Up @@ -216,7 +217,7 @@ private int invokeGetAlgorithmInfo(Frame frame, StringHandle hName, EnumHandle h
}
catch (GeneralSecurityException e)
{
return frame.raiseException(e.getMessage());
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand Down Expand Up @@ -282,7 +283,7 @@ public static Key extractKey(Frame frame, ObjectHandle hKey, String sAlgorithm,
}
}

enum KeyForm {Public, Private, PublicOrSecret, PrivateOrSecret}
public enum KeyForm {Public, Private, PublicOrSecret, PrivateOrSecret}


// ----- handles -------------------------------------------------------------------------------
Expand Down
Expand Up @@ -17,6 +17,7 @@
import org.xvm.runtime.Container;
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.Runtime;

import org.xvm.runtime.template.xException;
import org.xvm.runtime.template.xService;
Expand Down Expand Up @@ -222,7 +223,8 @@ private int runCommand(Frame frame, String sInput, String... cmd)

if (sError != null)
{
return frame.raiseException(xException.ioException(frame, sError));
return frame.raiseException(xException.ioException(frame,
Runtime.logRuntimeException(sError)));
}
}

Expand All @@ -232,7 +234,7 @@ private int runCommand(Frame frame, String sInput, String... cmd)
{
return frame == null
? Op.R_NEXT
: frame.raiseException(e.getMessage());
: frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.xvm.runtime.Container;
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.Runtime;

import org.xvm.runtime.template.xService;

Expand Down Expand Up @@ -93,7 +94,7 @@ private int invokeEncrypt(Frame frame, CipherHandle hCipher, ObjectHandle hKey,
}
catch (GeneralSecurityException e)
{
return frame.raiseException(e.getMessage());
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand All @@ -120,7 +121,7 @@ private int invokeDecrypt(Frame frame, CipherHandle hCipher, ObjectHandle hKey,
}
catch (GeneralSecurityException e)
{
return frame.raiseException(e.getMessage());
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}
}
Expand Up @@ -52,6 +52,7 @@
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.ObjectHandle.DeferredCallHandle;
import org.xvm.runtime.ObjectHandle.GenericHandle;
import org.xvm.runtime.Runtime;
import org.xvm.runtime.ServiceContext;
import org.xvm.runtime.TypeComposition;
import org.xvm.runtime.Utils;
Expand Down Expand Up @@ -105,7 +106,7 @@ public void initNative()
markNativeMethod("getPasswordInfo" , STRING, null);

ConstantPool pool = pool();
s_typeNamedPasssord = pool.ensureClassConstant(
s_typeNamedPassword = pool.ensureClassConstant(
pool.ensureModuleConstant("crypto.xtclang.org"), "CryptoPassword").getType();

invalidateTypeInfo();
Expand Down Expand Up @@ -214,7 +215,7 @@ public ObjectHandle ensureKeyStore(Frame frame, ObjectHandle hOpts)
catch (Exception e)
{
return new DeferredCallHandle(xException.makeHandle(frame,
"Illegal KeyStore arguments: " + e.getMessage()));
Runtime.logRuntimeException("Illegal KeyStore arguments: " + e.getMessage())));
}
}

Expand All @@ -235,7 +236,7 @@ public int invokeNativeGet(Frame frame, String sPropName, ObjectHandle hTarget,
}
catch (KeyStoreException e)
{
return frame.raiseException(xException.makeHandle(frame, e.getMessage()));
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}
}
Expand Down Expand Up @@ -375,7 +376,7 @@ private int invokeGetCertificateInfo(Frame frame, KeyStoreHandle hStore, StringH
}
catch (GeneralSecurityException e)
{
return frame.raiseException(xException.makeHandle(frame, e.getMessage()));
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand Down Expand Up @@ -452,7 +453,7 @@ private int invokeEntryType(Frame frame, KeyStoreHandle hStore, StringHandle hNa
}
catch (GeneralSecurityException e)
{
return frame.raiseException(xException.makeHandle(frame, e.getMessage()));
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand Down Expand Up @@ -517,7 +518,7 @@ private int invokeGetKeyInfo(Frame frame, KeyStoreHandle hStore, StringHandle hN
}
catch (GeneralSecurityException e)
{
return frame.raiseException(xException.makeHandle(frame, e.getMessage()));
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand Down Expand Up @@ -547,7 +548,7 @@ private int invokeGetPasswordInfo(Frame frame, KeyStoreHandle hStore, StringHand
}
catch (GeneralSecurityException e)
{
return frame.raiseException(xException.makeHandle(frame, e.getMessage()));
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand All @@ -566,7 +567,7 @@ public static StringHandle getPassword(Frame frame, ObjectHandle hPwd)
return hString;
}

hPwd = hPwd.revealAs(frame, s_typeNamedPasssord);
hPwd = hPwd.revealAs(frame, s_typeNamedPassword);
if (hPwd instanceof GenericHandle hNamed)
{
return (StringHandle) hNamed.getField(null, "password");
Expand Down Expand Up @@ -633,5 +634,5 @@ public Key getKey(String sName)
/**
* Cached NamedPassword type.
*/
private static TypeConstant s_typeNamedPasssord;
private static TypeConstant s_typeNamedPassword;
}
Expand Up @@ -12,6 +12,7 @@
import org.xvm.runtime.Container;
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.Runtime;

import org.xvm.runtime.template.xBoolean;
import org.xvm.runtime.template.xService;
Expand Down Expand Up @@ -96,7 +97,7 @@ private int invokeSign(Frame frame, SignatureHandle hSignature, ObjectHandle hKe
}
catch (GeneralSecurityException e)
{
return frame.raiseException(e.getMessage());
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}

Expand Down Expand Up @@ -124,7 +125,7 @@ private int invokeVerify(Frame frame, SignatureHandle hSignature, ObjectHandle h
}
catch (GeneralSecurityException e)
{
return frame.raiseException(e.getMessage());
return frame.raiseException(Runtime.logRuntimeException(e.getMessage()));
}
}
}
Expand Up @@ -55,6 +55,7 @@
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.ObjectHandle.JavaLong;
import org.xvm.runtime.Runtime;
import org.xvm.runtime.ServiceContext;
import org.xvm.runtime.TypeComposition;
import org.xvm.runtime.Utils;
Expand Down Expand Up @@ -309,7 +310,8 @@ private int invokeBind(Frame frame, HttpServerHandle hServer, ObjectHandle[] ahA
catch (Exception e)
{
frame.f_context.f_container.terminate(hServer.f_context);
return frame.raiseException(xException.ioException(frame, e.getMessage()));
return frame.raiseException(
xException.ioException(frame, Runtime.logRuntimeException(e.getMessage())));
}
}

Expand Down Expand Up @@ -608,7 +610,8 @@ private int invokeRespond(Frame frame, ObjectHandle[] ahArg)
}
catch (IOException e)
{
return frame.raiseException(xException.ioException(frame, e.getMessage()));
return frame.raiseException(
xException.ioException(frame, Runtime.logRuntimeException(e.getMessage())));
}

if (cbBody > 0)
Expand All @@ -619,7 +622,8 @@ private int invokeRespond(Frame frame, ObjectHandle[] ahArg)
}
catch (Throwable e)
{
return frame.raiseException(xException.ioException(frame, e.getMessage()));
return frame.raiseException(
xException.ioException(frame, Runtime.logRuntimeException(e.getMessage())));
}
}
return Op.R_NEXT;
Expand Down
Expand Up @@ -25,6 +25,7 @@
import org.xvm.runtime.Container;
import org.xvm.runtime.Frame;
import org.xvm.runtime.ObjectHandle;
import org.xvm.runtime.Runtime;
import org.xvm.runtime.Utils;

import org.xvm.runtime.template.xBoolean;
Expand Down Expand Up @@ -165,7 +166,8 @@ protected int invokeReadLine(Frame frame, ObjectHandle[] ahArg, int iReturn)
}
catch (IOException e)
{
return frame.raiseException(xException.ioException(frame, e.getMessage()));
return frame.raiseException(xException.ioException(frame,
Runtime.logRuntimeException(e.getMessage())));
}
}
else
Expand Down

0 comments on commit 7bc3668

Please sign in to comment.