Skip to content

Commit f0b6d2b

Browse files
committed
moved logging into a separate file
1 parent eb898e5 commit f0b6d2b

File tree

7 files changed

+116
-96
lines changed

7 files changed

+116
-96
lines changed

examples/authorize_and_store_papi.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { createClient } from 'polkadot-api';
33
import { getWsProvider } from 'polkadot-api/ws-provider';
44
import { cryptoWaitReady } from '@polkadot/util-crypto';
55
import { authorizeAccount, fetchCid, store, TX_MODE_FINALIZED_BLOCK } from './api.js';
6-
import { setupKeyringAndSigners, logHeader, logConnection, logSuccess, logError, logTestResult } from './common.js';
6+
import { setupKeyringAndSigners } from './common.js';
7+
import { logHeader, logConnection, logSuccess, logError, logTestResult } from './logger.js';
78
import { cidFromBytes } from "./cid_dag_metadata.js";
89
import { bulletin } from './.papi/descriptors/dist/index.mjs';
910

examples/authorize_and_store_papi_smoldot.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ import { createClient } from 'polkadot-api';
55
import { getSmProvider } from 'polkadot-api/sm-provider';
66
import { cryptoWaitReady } from '@polkadot/util-crypto';
77
import { authorizeAccount, fetchCid, store } from './api.js';
8-
import { setupKeyringAndSigners, waitForChainReady, logHeader, logConfig, logSuccess, logError, logTestResult } from './common.js';
8+
import { setupKeyringAndSigners, waitForChainReady } from './common.js';
9+
import { logHeader, logConfig, logSuccess, logError, logTestResult } from './logger.js';
910
import { cidFromBytes } from "./cid_dag_metadata.js";
1011
import { bulletin } from './.papi/descriptors/dist/index.mjs';
1112

examples/authorize_preimage_and_store_papi.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { createClient } from 'polkadot-api';
33
import { getWsProvider } from 'polkadot-api/ws-provider';
44
import { cryptoWaitReady } from '@polkadot/util-crypto';
55
import { authorizeAccount, authorizePreimage, fetchCid, store, TX_MODE_IN_BLOCK } from './api.js';
6-
import { setupKeyringAndSigners, getContentHash, logHeader, logConnection, logSection, logSuccess, logError, logInfo, logTestResult } from './common.js';
6+
import { setupKeyringAndSigners, getContentHash } from './common.js';
7+
import { logHeader, logConnection, logSection, logSuccess, logError, logInfo, logTestResult } from './logger.js';
78
import { cidFromBytes } from "./cid_dag_metadata.js";
89
import { bulletin } from './.papi/descriptors/dist/index.mjs';
910

examples/common.js

Lines changed: 0 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -10,97 +10,6 @@ export const HTTP_IPFS_API = 'http://127.0.0.1:8080'; // Local IPFS HTTP gatew
1010
export const CHUNK_SIZE = 1 * 1024 * 1024; // 1 MiB
1111
// -----------------
1212

13-
// ============================================================================
14-
// Unified Logging Functions
15-
// ============================================================================
16-
17-
/**
18-
* Print a section header
19-
*/
20-
export function logHeader(text) {
21-
console.log('\n' + '═'.repeat(80));
22-
console.log(` ${text}`);
23-
console.log('═'.repeat(80));
24-
}
25-
26-
/**
27-
* Print a sub-section header
28-
*/
29-
export function logSection(text) {
30-
console.log('\n' + '─'.repeat(80));
31-
console.log(` ${text}`);
32-
console.log('─'.repeat(80));
33-
}
34-
35-
/**
36-
* Log test configuration parameters
37-
*/
38-
export function logConfig(config) {
39-
console.log('\n📋 Configuration:');
40-
for (const [key, value] of Object.entries(config)) {
41-
console.log(` ${key.padEnd(20)}: ${value}`);
42-
}
43-
}
44-
45-
/**
46-
* Log connection information
47-
*/
48-
export function logConnection(wsUrl, seed, ipfsApi) {
49-
logConfig({
50-
'RPC Endpoint': wsUrl,
51-
'Account/Seed': seed,
52-
'IPFS API': ipfsApi
53-
});
54-
}
55-
56-
/**
57-
* Log a step in the process
58-
*/
59-
export function logStep(step, message) {
60-
console.log(`\n${step} ${message}`);
61-
}
62-
63-
/**
64-
* Log success message
65-
*/
66-
export function logSuccess(message) {
67-
console.log(`✅ ${message}`);
68-
}
69-
70-
/**
71-
* Log error message
72-
*/
73-
export function logError(message) {
74-
console.error(`❌ ${message}`);
75-
}
76-
77-
/**
78-
* Log info message
79-
*/
80-
export function logInfo(message) {
81-
console.log(`ℹ️ ${message}`);
82-
}
83-
84-
/**
85-
* Log warning message
86-
*/
87-
export function logWarning(message) {
88-
console.log(`⚠️ ${message}`);
89-
}
90-
91-
/**
92-
* Log final test result
93-
*/
94-
export function logTestResult(passed, testName = 'Test') {
95-
console.log('\n' + '═'.repeat(80));
96-
if (passed) {
97-
console.log(` ✅✅✅ ${testName} PASSED! ✅✅✅`);
98-
} else {
99-
console.log(` ❌❌❌ ${testName} FAILED! ❌❌❌`);
100-
}
101-
console.log('═'.repeat(80) + '\n');
102-
}
103-
10413
/**
10514
* Creates a PAPI-compatible signer from a Keyring account
10615
*/

examples/logger.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/**
2+
* Unified logging functions for example scripts
3+
* Provides consistent formatting and visual hierarchy across all tests
4+
*/
5+
6+
/**
7+
* Print a section header with double-line border
8+
* @param {string} text - Header text
9+
*/
10+
export function logHeader(text) {
11+
console.log('\n' + '═'.repeat(80));
12+
console.log(` ${text}`);
13+
console.log('═'.repeat(80));
14+
}
15+
16+
/**
17+
* Print a sub-section header with single-line border
18+
* @param {string} text - Section text
19+
*/
20+
export function logSection(text) {
21+
console.log('\n' + '─'.repeat(80));
22+
console.log(` ${text}`);
23+
console.log('─'.repeat(80));
24+
}
25+
26+
/**
27+
* Log configuration parameters in a formatted table
28+
* @param {Object} config - Configuration object with key-value pairs
29+
*/
30+
export function logConfig(config) {
31+
console.log('\n📋 Configuration:');
32+
for (const [key, value] of Object.entries(config)) {
33+
console.log(` ${key.padEnd(20)}: ${value}`);
34+
}
35+
}
36+
37+
/**
38+
* Log connection information (convenience function)
39+
* @param {string} wsUrl - WebSocket URL
40+
* @param {string} seed - Account seed or address
41+
* @param {string} ipfsApi - IPFS API URL
42+
*/
43+
export function logConnection(wsUrl, seed, ipfsApi) {
44+
logConfig({
45+
'RPC Endpoint': wsUrl,
46+
'Account/Seed': seed,
47+
'IPFS API': ipfsApi
48+
});
49+
}
50+
51+
/**
52+
* Log a step in the process
53+
* @param {string} step - Step indicator (e.g., "1️⃣", "2️⃣")
54+
* @param {string} message - Step description
55+
*/
56+
export function logStep(step, message) {
57+
console.log(`\n${step} ${message}`);
58+
}
59+
60+
/**
61+
* Log success message
62+
* @param {string} message - Success message
63+
*/
64+
export function logSuccess(message) {
65+
console.log(`✅ ${message}`);
66+
}
67+
68+
/**
69+
* Log error message
70+
* @param {string} message - Error message
71+
*/
72+
export function logError(message) {
73+
console.error(`❌ ${message}`);
74+
}
75+
76+
/**
77+
* Log info message
78+
* @param {string} message - Info message
79+
*/
80+
export function logInfo(message) {
81+
console.log(`ℹ️ ${message}`);
82+
}
83+
84+
/**
85+
* Log warning message
86+
* @param {string} message - Warning message
87+
*/
88+
export function logWarning(message) {
89+
console.log(`⚠️ ${message}`);
90+
}
91+
92+
/**
93+
* Log final test result with banner
94+
* @param {boolean} passed - Whether the test passed
95+
* @param {string} testName - Name of the test (default: "Test")
96+
*/
97+
export function logTestResult(passed, testName = 'Test') {
98+
console.log('\n' + '═'.repeat(80));
99+
if (passed) {
100+
console.log(` ✅✅✅ ${testName} PASSED! ✅✅✅`);
101+
} else {
102+
console.log(` ❌❌❌ ${testName} FAILED! ❌❌❌`);
103+
}
104+
console.log('═'.repeat(80) + '\n');
105+
}

examples/store_big_data.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ import {
1414
fileToDisk,
1515
filesAreEqual,
1616
generateTextImage,
17+
} from "./common.js";
18+
import {
1719
logHeader,
1820
logConnection,
1921
logStep,
2022
logSuccess,
2123
logError,
2224
logTestResult,
23-
} from "./common.js";
25+
} from "./logger.js";
2426
import { createClient } from 'polkadot-api';
2527
import { getWsProvider } from "polkadot-api/ws-provider";
2628
import { bulletin } from './.papi/descriptors/dist/index.mjs';

examples/store_chunked_data.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import { CID } from 'multiformats/cid'
66
import * as dagPB from '@ipld/dag-pb'
77
import { TextDecoder } from 'util'
88
import assert from "assert";
9-
import { generateTextImage, filesAreEqual, fileToDisk, setupKeyringAndSigners, logHeader, logConnection, logSuccess, logError, logTestResult } from './common.js'
9+
import { generateTextImage, filesAreEqual, fileToDisk, setupKeyringAndSigners } from './common.js'
10+
import { logHeader, logConnection, logSuccess, logError, logTestResult } from './logger.js'
1011
import { authorizeAccount, fetchCid, store, storeChunkedFile, TX_MODE_FINALIZED_BLOCK } from "./api.js";
1112
import { buildUnixFSDagPB, cidFromBytes, convertCid } from "./cid_dag_metadata.js";
1213
import { createClient } from 'polkadot-api';

0 commit comments

Comments
 (0)