/**
 * @license
 * Copyright 2017 The FOAM Authors. All Rights Reserved.
 * http://www.apache.org/licenses/LICENSE-2.0
 */

package foam.nanos.boot;

import foam.core.*;
import foam.nanos.*;
import foam.nanos.pm.PM;

public class NSpecFactory
  implements XFactory
{
  NSpec   spec_;
  ProxyX  x_;
  boolean isCreating_ = false;

  public NSpecFactory(ProxyX x, NSpec spec) {
    x_    = x;
    spec_ = spec;
  }

  public Object create(X x) {
    // Avoid infinite recursions when creating services
    if ( isCreating_ ) {
      return null;
    }

    isCreating_ = true;

    Object ns = null;
    PM     pm = new PM(this.getClass(), spec_ == null ? "-" : spec_.getName());

    try {
      ns = spec_.createService(x_.getX());

      if ( ns instanceof ContextAware ) ((ContextAware) ns).setX(x_.getX());
      if ( ns instanceof NanoService  ) ((NanoService)  ns).start();
    } catch (Throwable t) {
      // TODO: LOG
      t.printStackTrace();
    } finally {
      pm.log(x_.getX());
      isCreating_ = false;
    }

    return ns;
  }
}

@@ -208,16 +208,49 @@
 s.pm.PM;
+%0Aimport foam.nanos.logger.Logger;
 %0A%0Apublic
@@ -616,16 +616,20 @@
 ject ns 
+    
 = null;%0A
@@ -642,16 +642,20 @@
      pm 
+    
 = new PM
@@ -716,57 +716,258 @@
 ));%0A
-%0A    try %7B%0A      ns = spec_.createService(x_.getX
+    Logger logger = (Logger) x.get(%22logger%22);%0A%0A    try %7B%0A      if ( logger != null ) logger.info(%22Creating Service%22, spec_.getName());%0A      ns = spec_.createService(x_.getX());%0A      if ( logger != null ) logger.info(%22Created Service%22, spec_.getName
 ());
@@ -1152,45 +1152,87 @@
     
-// TODO: LOG%0A      t.printStackTrace(
+if ( logger != null ) logger.error(%22Error Creating Service%22, spec_.getName(), t
 );%0A 

Add logging to NSpecFactory.