question

BillS avatar image
BillS asked BillS posted

Example of using IContext to determine headless/GUi mode?

For a reporting plugin I have a logging routine which writes data to a plugin-specific console.

 

I'd like to adapt so it dumps to a different location (stdout, a log file, etc) if running from itestcli in headless mode, and looking for a pointer on where to get that data from. My assumption is that within onReportStart I can leverage IContext to do it, but am not clear on the proper casting, etc.

 

Any pointers on how I can get that mode so I can branch accordingly?

 

Thanks,

-Bill

 

 

iTestsdk
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

·
PaulD avatar image
PaulD answered PaulD posted

I believe the easiest way here is 

 

if (Display.getCurrent() == null) {

  // you are running headless

}

8 comments
10 |950

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

BillS avatar image BillS commented ·

Unfortunately the plugin I'm working on doesn't make contributions to the Display or UI in general, so Display.getCurrent() is always null.

0 Likes 0 ·
PaulD avatar image PaulD BillS commented ·

You're right.  I forgot.  Display.getCurrent() returns null unless you are actually running inside the UI thread.  And you don't want to call Display.getDefault() because it will create a display if there isn't already one running -- which you don't want to happen when running headless.

 

Hmmmmm.  Interesting.  I thought this was easy and it doesn't appear to be!

 

Internally, we keep around a mode flag that tells us whether we're running headless or not.  That's because Eclipse doesn't have the notion of running without a UI.  Unfortunately, I don't see an easy way for you to get access to that mode.

 

Let us take a look...

0 Likes 0 ·
AdamB avatar image AdamB PaulD commented ·

I'm curious, what behavior differences would you like to have running headless v.s. w/ a UI?  There might be other ways to skin this cat.  (or a work around while we get a cleaner solution.)

0 Likes 0 ·
BillS avatar image BillS AdamB commented ·

In this case it's message logging path - I'm logging results from a conversion and RESTful service call to a console window if running through the GUI, and want to provide an alternate log path (stdout, file, etc) if there's no visible console.

 

Technically I can have people run with the Eclipse arg -consolelog, but...

0 Likes 0 ·
AdamB avatar image AdamB BillS commented ·

Are people running iTestCLI or iTestRT in headless mode?

0 Likes 0 ·
BillS avatar image BillS AdamB commented ·

Yes. They may run either, and I have no control over which they choose.

0 Likes 0 ·
PaulD avatar image PaulD BillS commented ·

Would it make sense to log to both (at least as a short-term solution)?

0 Likes 0 ·
KumarS avatar image KumarS PaulD commented ·

I read some articles online and it seems that people prefer the following way to determine the headless mode:

 

PlatformUI.isWorkbenchRunning() or

PlatformUI.getWorkbench()

 

So it would seem to me that if one follows normal Eclipse guidelines and breaks up the code into "foo.core" and "foo.ui" plugins, then it becomes easy to support headless and GUI mode. "foo.ui" plugin extends something in "foo.core" and "foo.core" notifies "foo.ui" if it is present. "foo.ui" before doing anything checks if PlatformUI.isWorkbenchRunning() returns true. If it returns false, foo.ui does nothing.

 

 

0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.