0

Where can I find documentation about Eiffel introspection?

Some functions can be called in the GENERAL Class

I'm looking for the caller's class name to modify the logger's formatter

Something like:

Current.generator_client_object.class_name
Pipo
  • 4,653
  • 38
  • 47

1 Answers1

1

It's possible to lookup for a class name of an object with generator. More detailed information can be obtained with other means, sorted from higher level to lower level:

  • calling a feature generating_type that returns an object of type TYPE
  • using a descendant of class REFLECTED_OBJECT (there are versions for a reference object and for an expanded object) to dig into an object structure
  • using classes REFLECTOR or INTERNAL for lower-level manipulations with less abstraction

There is no standard mechanism to fetch the details of the currently executing feature or its caller. It still might be possible to obtain this information from the exception stack trace. The idea is as follows:

  1. Add a feature that will actually do the logging.
  2. Add some code to this feature that will raise an exception.
  3. Catch the exception in the rescue clause of the feature.
  4. Parse the stack trace retrieved with {EXCEPTION}.trace one or two level up (some experiments are needed to get the correct result).
  5. Log the information about the caller (class + feature + stop point number).

Although, this is feasible, the performance is going to be an issue, because exception handling and parsing are slow operations.

Alexander Kogtenkov
  • 5,770
  • 1
  • 27
  • 35
  • Thx, it helps, but still don't know where I can find documentation about that in general, and how to get the caller from generating_type – Pipo Oct 01 '18 at 12:25
  • 1
    @Pipo I'm afraid it's impossible to figure out who is calling a feature unless the caller tells it explicitly either by passing its `Current` or the associated class name. – Alexander Kogtenkov Oct 01 '18 at 14:37
  • and from execution environment how could it be? my goal is to format a logger, the ideal would be class->method->line->level something like that... – Pipo Oct 01 '18 at 17:34
  • @Pipo There is no efficient way to do that. I've updated the answer with a possible solution though. – Alexander Kogtenkov Oct 01 '18 at 18:39
  • Many thx, as it is a workaround, I think I let the answer open... but your workaround seems good, strange because loggers are quite standard and useful library tool today in almost all languages/standard_libraries – Pipo Oct 01 '18 at 19:03
  • 1
    @Pipo Indeed, there is similar functionality in [.NET since 4.5](https://stackoverflow.com/questions/12556767/how-do-i-get-the-current-line-number) and [Java](https://stackoverflow.com/questions/115008/how-can-we-print-line-numbers-to-the-log-in-java). The latter uses a similar workaround, though a bit simpler due to an abstraction layer on top of the call stack representation. The workaround can be integrated into Eiffel libraries as soon as someone comes with the corresponding patch. – Alexander Kogtenkov Oct 02 '18 at 05:16
  • So How does the .out function from ANY return the object class name???!!! – Pipo Oct 09 '18 at 18:40
  • @Pipo Every object has the associated type based on a class. The implementation of the feature `out` can retrieve this information at run-time, and report it. – Alexander Kogtenkov Oct 10 '18 at 06:18