34
34
import com .thefirstlineofcode .chalk .core .StandardChatClient ;
35
35
import com .thefirstlineofcode .chalk .core .stream .StandardStreamConfig ;
36
36
import com .thefirstlineofcode .chalk .core .stream .UsernamePasswordToken ;
37
+ import com .thefirstlineofcode .chalk .logger .LogConfigurator ;
37
38
import com .thefirstlineofcode .chalk .network .ConnectionException ;
38
39
import com .thefirstlineofcode .chalk .network .IConnectionListener ;
39
40
import com .thefirstlineofcode .sand .client .ibtr .IRegistration ;
@@ -49,7 +50,7 @@ public abstract class AbstractEdgeThing extends AbstractThing implements IEdgeTh
49
50
private static final String INTERNET_CONNECTIVITY_TEST_ADDRESS = "http://www.baidu.com" ;
50
51
private static final String SAND_EDGE_CONFIG_DIR = ".com.thefirstlineofcode.sand.client.edge" ;
51
52
52
- private static final Logger logger = LoggerFactory .getLogger (AbstractEdgeThing .class );
53
+ private static final Logger logger = LogConfigurator . isConfigured () ? LoggerFactory .getLogger (AbstractEdgeThing .class ) : null ;
53
54
54
55
protected StandardStreamConfig streamConfig ;
55
56
protected RegisteredThing registeredThing ;
@@ -108,7 +109,9 @@ protected StandardStreamConfig getStreamConfig(Map<String, String> attributes) {
108
109
109
110
StringTokenizer st = new StringTokenizer (sStreamConfig , "," );
110
111
if (st .countTokens () != 3 ) {
111
- logger .error ("Can't read stream config. Not a valid stream config string." );
112
+ if (LogConfigurator .isConfigured ())
113
+ logger .error ("Can't read stream config. Not a valid stream config string." );
114
+
112
115
throw new IllegalArgumentException ("Can't read stream config. Not a valid stream config string." );
113
116
}
114
117
@@ -145,7 +148,8 @@ public void start() {
145
148
try {
146
149
doStart ();
147
150
} catch (Exception e ) {
148
- logger .error ("Some thing is wrong. The program can't run correctly." , e );
151
+ if (LogConfigurator .isConfigured ())
152
+ logger .error ("Some thing is wrong. The program can't run correctly." , e );
149
153
150
154
throw new RuntimeException ("Some thing is wrong. The program can't run correctly." , e );
151
155
}
@@ -178,8 +182,13 @@ protected void doStart() {
178
182
if (streamConfig == null )
179
183
throw new IllegalStateException ("Null stream config." );
180
184
181
- logger .info ("I'm an edge thing[thing_id='{}', host='{}', port='{}', tls_preferred='{}']." ,
182
- thingId , this .streamConfig .getHost (), this .streamConfig .getPort (), this .streamConfig .isTlsPreferred ());
185
+ if (LogConfigurator .isConfigured ()) {
186
+ logger .info ("I'm an edge thing[thing_id='{}', host='{}', port='{}', tls_preferred='{}']." ,
187
+ thingId , this .streamConfig .getHost (), this .streamConfig .getPort (), this .streamConfig .isTlsPreferred ());
188
+ } else {
189
+ System .out .println (String .format ("I'm an edge thing[thing_id='%s', host='%s', port='%s', tls_preferred='%s']." ,
190
+ thingId , this .streamConfig .getHost (), this .streamConfig .getPort (), this .streamConfig .isTlsPreferred ()));
191
+ }
183
192
184
193
if (!isHostLocalLanAddress ()) {
185
194
checkInternetConnectivity (10 );
@@ -203,7 +212,11 @@ protected void doStart() {
203
212
connect ();
204
213
}
205
214
206
- logger .info ("The thing has started." );
215
+ if (LogConfigurator .isConfigured ())
216
+ logger .info ("The thing has started." );
217
+ else
218
+ System .out .println ("The thing has started." );
219
+
207
220
started = true ;
208
221
209
222
if (startConsole ) {
@@ -217,7 +230,10 @@ private void checkInternetConnectivity(int retryTimes) {
217
230
while (!checkInternetConnectivity ()) {
218
231
i ++;
219
232
220
- logger .info ("No internet connection. Waiting for a while then trying again...." );
233
+ if (LogConfigurator .isConfigured ())
234
+ logger .info ("No internet connection. Waiting for a while then trying again...." );
235
+ else
236
+ System .out .println ("No internet connection. Waiting for a while then trying again...." );
221
237
222
238
try {
223
239
Thread .sleep (2000 );
@@ -227,7 +243,11 @@ private void checkInternetConnectivity(int retryTimes) {
227
243
}
228
244
229
245
if (i == retryTimes ) {
230
- logger .error ("No internet connection. The thing can't be started." );
246
+ if (LogConfigurator .isConfigured ())
247
+ logger .error ("No internet connection. The thing can't be started." );
248
+ else
249
+ System .out .println ("No internet connection. The thing can't be started." );
250
+
231
251
throw new IllegalStateException ("No internet connection. The program will exit." );
232
252
}
233
253
}
@@ -315,7 +335,10 @@ public void connect() {
315
335
chatClient .addConnectionListener (this );
316
336
}
317
337
318
- logger .info ("The thing tries to connect to server." );
338
+ if (LogConfigurator .isConfigured ())
339
+ logger .info ("The thing tries to connect to server." );
340
+ else
341
+ System .out .println ("The thing tries to connect to server." );
319
342
320
343
try {
321
344
chatClient .connect (new UsernamePasswordToken (registeredThing .getThingName (),
@@ -385,7 +408,10 @@ protected void registered(RegisteredThing registeredThing) {
385
408
386
409
this .registeredThing = registeredThing ;
387
410
388
- logger .info ("The thing has registered. Thing name is '{}'." , registeredThing .getThingName ());
411
+ if (LogConfigurator .isConfigured ())
412
+ logger .info ("The thing has registered. Thing name is '{}'." , registeredThing .getThingName ());
413
+ else
414
+ System .out .println (String .format ("The thing has registered. Thing name is '%s'." , registeredThing .getThingName ()));
389
415
}
390
416
391
417
@ Override
@@ -402,9 +428,12 @@ public void stop() {
402
428
disconnect ();
403
429
}
404
430
405
- logger .info ("The thing has stopped." );
406
- started = false ;
431
+ if (LogConfigurator .isConfigured ())
432
+ logger .info ("The thing has stopped." );
433
+ else
434
+ System .out .println ("The thing has stopped." );
407
435
436
+ started = false ;
408
437
stopConsoleThread ();
409
438
}
410
439
@@ -450,7 +479,10 @@ public void register() {
450
479
IChatClient chatClient = new StandardChatClient (streamConfig );
451
480
chatClient .register (IbtrPlugin .class );
452
481
453
- logger .info ("The thing tries to register to server." );
482
+ if (LogConfigurator .isConfigured ())
483
+ logger .info ("The thing tries to register to server." );
484
+ else
485
+ System .out .println ("The thing tries to register to server." );
454
486
455
487
IRegistration registration = null ;
456
488
try {
@@ -550,8 +582,10 @@ public void run() {
550
582
551
583
synchronized (AbstractEdgeThing .this ) {
552
584
if (!isConnected ()) {
553
- if (logger . isInfoEnabled ())
585
+ if (LogConfigurator . isConfigured ())
554
586
logger .info ("The thing has disconnected. Try to reconnect to server...." );
587
+ else
588
+ System .out .println ("The thing has disconnected. Try to reconnect to server...." );
555
589
556
590
connect ();
557
591
}
@@ -572,8 +606,10 @@ public void run() {
572
606
}
573
607
574
608
protected void connected (IChatClient chatClient ) {
575
- if (logger . isInfoEnabled ())
609
+ if (LogConfigurator . isConfigured ())
576
610
logger .info ("The thing has connected to server." );
611
+ else
612
+ System .out .println ("The thing has connected to server." );
577
613
578
614
startIotComponents ();
579
615
startAutoReconnectThread ();
@@ -583,24 +619,37 @@ protected void connected(IChatClient chatClient) {
583
619
}
584
620
585
621
protected void FailedToConnect (ConnectionException e ) {
586
- logger .error ("The thing failed to connect to server." , e );
622
+ if (LogConfigurator .isConfigured ()) {
623
+ logger .error ("The thing failed to connect to server." , e );
624
+ } else {
625
+ System .out .println ("The thing failed to connect to server." );
626
+ e .printStackTrace ();
627
+ }
587
628
}
588
629
589
630
protected void failedToAuth (AuthFailureException e ) {
590
- logger .error ("The thing failed to auth to server." , e );
631
+ if (LogConfigurator .isConfigured ())
632
+ logger .error ("The thing failed to auth to server." , e );
591
633
592
634
throw new RuntimeException ("Failed to auth to server." , e );
593
635
}
594
636
595
637
protected void disconnected () {
596
638
stopIotComponents ();
597
639
598
- if (logger . isInfoEnabled ())
640
+ if (LogConfigurator . isConfigured ())
599
641
logger .info ("The thing has disconnected from server." );
642
+ else
643
+ System .out .println ("The thing has disconnected from server." );
600
644
}
601
645
602
646
protected void registrationExceptionOccurred (RegistrationException e ) {
603
- logger .error ("Registration exception occurred." , e );
647
+ if (LogConfigurator .isConfigured ()) {
648
+ logger .error ("Registration exception occurred." , e );
649
+ } else {
650
+ System .out .println ("Registration exception occurred." );
651
+ e .printStackTrace ();
652
+ }
604
653
}
605
654
606
655
protected RegisteredThing getRegisteredThing (Map <String , String > attributes ) {
@@ -678,7 +727,10 @@ protected Map<String, String> loadThingAttributes() {
678
727
Path attributesFilePath = getAttributesFilePath ();
679
728
680
729
if (!Files .exists (attributesFilePath , LinkOption .NOFOLLOW_LINKS )) {
681
- logger .info ("Attributes file not existed. Ignore to load attributes." );
730
+ if (LogConfigurator .isConfigured ())
731
+ logger .info ("Attributes file not existed. Ignore to load attributes." );
732
+ else
733
+ System .out .println ("Attributes file not existed. Ignore to load attributes." );
682
734
683
735
return null ;
684
736
}
@@ -689,8 +741,9 @@ protected Map<String, String> loadThingAttributes() {
689
741
reader = Files .newBufferedReader (attributesFilePath , Charset .forName (Constants .DEFAULT_CHARSET ));
690
742
properties .load (reader );
691
743
} catch (Exception e ) {
692
- logger .error (String .format ("Can't load attributes from file '%s'." ,
693
- attributesFilePath .toAbsolutePath ()), e );
744
+ if (LogConfigurator .isConfigured ())
745
+ logger .error ("Can't load attributes from file '{}'." , attributesFilePath .toAbsolutePath (), e );
746
+
694
747
throw new RuntimeException (String .format ("Can't load attributes from file '%s'." ,
695
748
attributesFilePath .toAbsolutePath ()), e );
696
749
} finally {
@@ -724,7 +777,9 @@ protected void saveAttributes(Map<String, String> attributes) {
724
777
try {
725
778
Files .move (attributesFilePath , attributesBakFilePath );
726
779
} catch (IOException e ) {
727
- logger .error ("Can't backup attributes file." , e );
780
+ if (LogConfigurator .isConfigured ())
781
+ logger .error ("Can't backup attributes file." , e );
782
+
728
783
throw new RuntimeException ("Can't backup attributes file." , e );
729
784
}
730
785
}
@@ -733,7 +788,9 @@ protected void saveAttributes(Map<String, String> attributes) {
733
788
try {
734
789
Files .createDirectories (attributesFilePath .getParent ());
735
790
} catch (IOException e ) {
736
- logger .error (String .format ("Can't create directory %s." , attributesFilePath .getParent ().toAbsolutePath ()), e );
791
+ if (LogConfigurator .isConfigured ())
792
+ logger .error ("Can't create directory {}." , attributesFilePath .getParent ().toAbsolutePath (), e );
793
+
737
794
throw new RuntimeException (String .format ("Can't create directory %s." , attributesFilePath .getParent ().toAbsolutePath ()), e );
738
795
}
739
796
}
@@ -744,10 +801,14 @@ protected void saveAttributes(Map<String, String> attributes) {
744
801
StandardOpenOption .CREATE , StandardOpenOption .WRITE );
745
802
properties .store (writer , null );
746
803
747
- logger .info (String .format ("Attributes are saved to %s." , attributesFilePath .toAbsolutePath ()));
804
+ if (LogConfigurator .isConfigured ())
805
+ logger .info ("Attributes are saved to {}." , attributesFilePath .toAbsolutePath ());
806
+ else
807
+ System .out .println (String .format ("Attributes are saved to %s." , attributesFilePath .toAbsolutePath ()));
748
808
} catch (Exception e ) {
749
- logger .error (String .format ("Can't save attributes to file '%s'." ,
750
- attributesFilePath .toAbsolutePath ()), e );
809
+ if (LogConfigurator .isConfigured ())
810
+ logger .error ("Can't save attributes to file '{}'." , attributesFilePath .toAbsolutePath (), e );
811
+
751
812
throw new RuntimeException (String .format ("Can't save attributes to file '%s'." ,
752
813
attributesFilePath .toAbsolutePath ()), e );
753
814
} finally {
@@ -764,7 +825,9 @@ protected void saveAttributes(Map<String, String> attributes) {
764
825
try {
765
826
Files .delete (attributesBakFilePath );
766
827
} catch (IOException e ) {
767
- logger .error ("Can't delete attributes backup file." , e );
828
+ if (LogConfigurator .isConfigured ())
829
+ logger .error ("Can't delete attributes backup file." , e );
830
+
768
831
throw new RuntimeException ("Can't delete attributes backup file." , e );
769
832
}
770
833
}
0 commit comments